简体   繁体   English

Java Selenium中多个类中Webdriver的相同实例

[英]Same instance of Webdriver in multiple classes in Java Selenium

Class that contains all classes 包含所有类的类

public class AllTests{
    public static void main(String[] args) {
        Loginer.login();
        Example.linkOne();
        Examplee.linkTwo();
    }
}

Class that starts the Firefox driver and logins 启动Firefox驱动程序并登录的类

public class Loginer{
    public static login(){
        WebDriver driver = new FirefoxDriver();
        driver.get("http://LINKISHERE.COM/");
        //other login code
    }
}

Actual Selenium code that clicks links and stuff 单击链接和内容的实际Selenium代码

public class Example{

    public static linkOne() {
            **driver**.findElement(By.className("CLASSNAME")).click();
    }

    public static linkTwo() {
            **driver**.findElement(By.className("CLASSNAME")).click();
    }
}

I'm pretty new to JAVA, I only worked with python till now. 我对JAVA还是很陌生,到目前为止我只使用python。 What I'm trying to do is have multiple tests split into multiple classes that are part of the AllTests class, so I can take them out or add new ones with easy. 我想做的是将多个测试分为多个类,这些类属于AllTests类,因此我可以轻松地将它们删除或添加新的类。 My trouble has been using the same WebDriver in all classes due to this java.lang.NullPointerException . 由于此java.lang.NullPointerException我一直在所有类中使用相同的WebDriver。 Is this recommended or should it be fine to have Selenium start a new WebDriver every time? 是建议这样做还是应该让Selenium每次都启动一个新的WebDriver?

You can change your classes as given below. 您可以按照以下说明更改课程。

public class Loginer{

  public static WebDriver driver;

  public static login(){

    driver = new FirefoxDriver();

    driver.get("http://LINKISHERE.COM/");
    //other login code
    }
}

public class Example{

 public static linkOne() {

        Loginer.driver.findElement(By.className("CLASSNAME")).click();

    }
}

public class Examplee{

 public static linkTwo() {

        Loginer.driver.findElement(By.className("CLASSNAME")).click();

    }
}

Here, I am storing the driver instance in a static variable and using it in all classes. 在这里,我将驱动程序实例存储在一个静态变量中,并在所有类中使用它。 It may works for you. 它可能适合您。

Initialize the driver in the AllTests class and pass it along to the others as a method argument. AllTests类中初始化驱动程序,并将其作为方法参数传递给其他驱动程序。

public class AllTests {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        Loginer.login(driver);
        Example.linkOne(driver);
        Examplee.linkTwo(driver);
    }
}

public class Loginer {
    public static void login(WebDriver driver){
        driver.get("http://LINKISHERE.COM/");
        // other login code
    }
}

public class Example { 
    public static void linkOne(WebDriver driver) {
        driver.findElement(By.className("CLASSNAME")).click();
    }
}

public class Examplee {  
    public static void linkTwo(WebDriver driver) {
        driver.findElement(By.className("CLASSNAME")).click();
    }
}

暂无
暂无

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

相关问题 如何在Selenium WebDriver中为多个类创建同一实例 - How to create a same instance for multiple classes in selenium webDriver 如何在java中使用TestNG Selenium webdriver在多个类中使用相同的浏览器窗口? - How to use the same browser window in multiple classes using TestNG Selenium webdriver in java? 使用相同的Firefox窗口在Selenium WebDriver(Java)中运行多个测试 - Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java) 如何使用带有Java的Selenium WebDriver多次单击同一按钮 - How to click on a same button multiple times using Selenium WebDriver with Java Selenium Webdriver通过多个类继续 - Selenium Webdriver continue through multiple classes 如何在Selenium Webdriver Java中设置多个代理 - How to set multiple proxies in Selenium Webdriver Java 在同一个中央项目文件夹上运行带有多台计算机的 selenium webdriver IntelliJ/Java/JUnit - Run selenium webdriver IntelliJ/Java/JUnit w/multiple computers on same central project folder 如何处理相同的多个窗口,例如Selenium WebDriver with Java中的google - How to handle same multiple windows e.g. google in Selenium WebDriver with Java 如何在多次运行相同脚本的同时使用 Java 在 selenium webdriver 中动态更改输入值 - How to change input values dynamically in selenium webdriver using Java while running the same script multiple times 获取不同类中的ThreadLocal变量 - Java - Selenium WebDriver - Get ThreadLocal Variable In Different Classes - Java - Selenium WebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM