简体   繁体   English

如何使用不同类别的同一个Webdriver

[英]How to use the same Webdriver from different class

I have 2 Java classes; 我有2个Java类; Main.java and Methods.java. Main.java和Methods.java。 At Main.java, I initialize the chrome webdriver and I want to use the same webdriver for a method at Methods.java. 在Main.java中,我初始化了chrome webdriver,并希望对Methods.java中的方法使用相同的webdriver。 Below are the codes. 以下是代码。

Under Main.java 在Main.java下

Methods getMethods = new Methods();

    @BeforeTest
    public void Setup()
    {
        System.setProperty("webdriver.chrome.driver", "C:\\...\\chromedriver.exe");

        driver = new ChromeDriver();

        driver.get(PropertiesConfig.getObject("websiteUrl"));

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);          
    }

  @Test
        public void TestCase1()
        {
          getMethods.method1();
        }


@AfterTest
    public void QuitTC() {
        getMethods.QuitTC(); }

Under Methods.java 在Methods.java下

    public void method1 (){
                  driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        …..  }

    public void QuitTC() {
        driver.quit();
    }

My question is how do I called the initialize Webdriver from Main.java and used it at Methods.java? 我的问题是如何从Main.java调用初始化Webdriver并在Methods.java中使用它?

Any help with be appreciated! 任何帮助,不胜感激! Thanks! 谢谢!

You can do something like this in a utility class (say TestUtil.java) 您可以在实用程序类中执行类似的操作(例如TestUtil.java)

private static WebDriver wd;

public static WebDriver getDriver() {
    return wd;
}

and then you can use following line to get the webdriver in any of the classes mentioned and work on it 然后您可以使用下面的代码行获取提到的任何类中的webdriver并对其进行处理

WebDriver driver = TestUtil.getDriver();

Declare a global variable for driver like this : 驱动程序声明一个全局变量,如下所示:

WebDriver driver = null; 

@BeforeTest
public void Setup()
{
    System.setProperty("webdriver.chrome.driver", "C:\\...\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.get(PropertiesConfig.getObject("websiteUrl"));

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);          
}  

Now, you can call method1 from method class like this : 现在,您可以像这样从方法类中调用method1:

public class Methods{

 public Methods(WebDriver driver){
      this.driver = driver; 
  }

public void method1 (){
                  driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        …..  }
}   

Now once you create the instance of Methods class, constructor would be called and driver reference could be passed. 现在,一旦创建了Methods类的实例,就将调用构造函数并可以传递驱动程序引用。

Try this 尝试这个

    Class1 {
    public WebDriver driver = null;
    public String baseURL="...";

    public void openURL() {
    System.setProperty("webdriver.chrome.driver", "D:...\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get(baseURL);
    }

    Class2 extends Class1 {
    driver.findElement(....);
    }

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

相关问题 如何在同一类的不同方法中使用显式等待对象? - How to use explicit wait object in different methods from the same class? 如何在同一应用程序中使用不同版本的 class? - How to use different versions of a class in the same application? 如何在一个班级的不同地方使用同一对象? - How to use same object at different places in a class? 我们如何在Selenium Webdriver中为不同的字段名称单击相同的类名ON按钮 - How we click same class name ON button for different field name in selenium webdriver 如何从不同的罐子加载相同的类 - how to load same class from different jars 如何使用selenium webdriver从firefox浏览器中提取和使用JSESSIONID cookie,以使用同一会话。 - How to Extract and use the JSESSIONID cookie from firefox browser using selenium webdriver to use same session. 如何在不同的List类中使用相同的内部类? - How to use same inner class in different List classes? JPA,如何使用相同的类(实体)来映射不同的表? - JPA, How to use the same class (entity) to map different tables? 如何在不同的SQL查询中使用相同的Java类 - How to use the same Java class with different SQL queries 如何在不同的类中使用相同的对象及其值 - How to use same object with its value in different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM