简体   繁体   English

使用Java从另一个类调用变量

[英]Call variable from another class using java

i have a code to test a web application using webdriver, the app contains more than 100 elements. 我有使用webdriver测试Web应用程序的代码,该应用程序包含100多个元素。 My testing code works fine but if one day if one of the element is modified i will have to change in all my code everywhere is use this element. 我的测试代码可以正常工作,但是如果某一天如果其中一个元素被修改,那么我将不得不在所有使用该元素的地方更改所有代码。 So i want to put all element as String in one class and then call one of the element from another class. 所以我想将所有元素作为String放在一个类中,然后从另一个类中调用其中一个元素。

How can do this? 怎么办呢? For example: my test code for gmail inbox: 例如:我的gmail收件箱测试代码:

public class ShowInbox {

    public ShowInbox(WebDriver driver) {


        driver.get(driver.getCurrentUrl());
        Utility.wait(3);
        String Inbox = "J-Ke n0";
        String MsgSuivis = "J-Ke n0 aBU";

        try {
        driver.findElement(By.className(Inbox)).isDisplayed();
        System.out.println("Boîte de réception exists");
        driver.findElement(By.className(Inbox)).click();
        } 
catch (NoSuchElementException e) {
System.out.println("Boite de réception is not displayed");
        } finally {
            System.out.println("continue");
        }

        try {

            driver.findElement(By.className(MsgSuivis)).isDisplayed();
            System.out.println("Messages suivis exists");
            driver.findElement(By.className(MsgSuivis)).click();
        } catch (NoSuchElementException e) {
            System.out.println("Messages suivis is not displayed");
        } finally {
            System.out.println("continue");
        }
        driver.quit();
    }
}

This code is working well when it runs. 该代码在运行时运行良好。 Now i want to create a class which will contain only String Inbox = "J-Ke n0"; 现在我想创建一个仅包含String Inbox =“ J-Ke n0”;的类。 String MsgSuivis = "J-Ke n0 aBU"; 字符串MsgSuivis =“ J-Ke n0 aBU”; And then in another i want to call for example only Inbox and then the code to test the inbox. 然后在另一个示例中,我只想调用收件箱,然后再调用测试收件箱的代码。

Any help please, thank you 任何帮助,谢谢

If I am not mistaken, you are looking for a separate class to store your constant strings. 如果我没记错的话,您正在寻找一个单独的类来存储常量字符串。 This is done by creating a new class and making the fields public. 这是通过创建一个新类并公开这些字段来完成的。 There are also a few good practices for this: 也有一些好的做法:

ElementNames.java ElementNames.java

public final class ElementNames {

    public static final String Inbox = "J-Ke n0";
    public static final String MsgSuivis = "J-Ke n0 aBU";
    // ... more names

    private ElementNames() { } // Private default constructor
}

Here, public allows access to the variables from outside the class. 在这里, public允许从类外部访问变量。 static allows access to the variables without creating an instance of the class. static允许访问变量而无需创建类的实例。 final is important so that the value is constant and cannot be modified after initialization. final很重要,因此该值是恒定的并且在初始化后不能修改。

Another common pattern is to create a private default constructor so that someone doesn't accidentaly create an instance of the constants class. 另一个常见的模式是创建一个私有的默认构造函数,这样就不会有人意外地创建常量类的实例。

To use the new constants: 要使用新常量:

ShowInbox.java ShowInbox.java

public class ShowInbox {

    public ShowInbox(WebDriver driver) {

        // ...
        driver.findElement(By.className(ElementNames.Inbox)).isDisplayed();
        System.out.println("Boîte de réception exists");
        driver.findElement(By.className(ElementNames.Inbox)).click();

        // ...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM