简体   繁体   English

如何在WebDriver类中初始化一次驱动程序,然后使用它来启动其他类?

[英]How to initialize driver once in WebDriver class and then use it to start other classes?

If I have 10 classes in testng.xml, it will open 10 browser sessions. 如果在testng.xml中有10个类,它将打开10个浏览器会话。 How can I initialize the driver once and only open one browser for one test case then close and open a window again for the second test case and so on? 如何初始化驱动程序一次,只为一个测试用例打开一个浏览器,然后为第二个测试用例再次关闭并打开一个窗口,依此类推?

I have my driver setup code in a constructor, which is maybe a bad way to approach it? 我在构造函数中有驱动程序安装代码,这也许是一种不好的处理方式? How can I initialize it in TNGDriver class and then use it in Testcase1 class? 如何在TNGDriver类中对其进行初始化,然后在Testcase1类中使用它?

I tried using @BeforeClass and have a setUp method but that did not work. 我尝试使用@BeforeClass并有一个setUp方法,但是没有用。

TNGDriver class TNGDriver类

public abstract class TNGDriver {

public static WebDriver driver;
private static String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe";  

@SuppressWarnings("deprecation")
public TNGDriver() {        
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty("webdriver.chrome.driver", chromeDriverPath);        
    driver = new ChromeDriver(capabilities);    
    driver.manage().window().maximize();            
}

public static WebDriver getDriver() {
    return driver;
}

public static void setDriver(WebDriver driver) {
    TNGDriver.driver = driver;
}

Testcase1 class Testcase1类

public class Testcase1 extends Registration {

    @Test(priority = 1) 
    public void step1_checkSomething() {        
        //do something
    }

    @Test(priority = 2)
    public void step2_clickOnSomething() {          
        //click on something            
    }

}

testng.xml 的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="AutomationFramework" parallel="false" preserve-order="true">
    <test name="Registration">      
        <classes>
            <class name="regression.Testcase01" />
            <class name="regression.Testcase02" />              
        </classes>      
    </test>             
</suite>

Chrome Driver opens every time private window by default, You don't need this really. 默认情况下,Chrome驱动程序会在每次打开私有窗口时打开,您实际上不需要这样做。

//options.addArguments("--incognito");

You can make structure like below : 您可以使结构如下所示:

public class TNGDriver {

public static WebDriver driver;
private static String chromeDriverPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe";  

public void DriverConfiguration() {        
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--incognito");  
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty("webdriver.chrome.driver", chromeDriverPath);        
    driver = new ChromeDriver(capabilities);    
    driver.manage().window().maximize();            
}

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

}

Unit Test cases: 单元测试用例:

public class Testcase1 extends Registration {

TNGDriver objTND = new TNGDriver();

    @BeforeTest
    public void initializeDriver(){
    objTND.DriverConfiguration();  
    }

    @Test(priority = 1) 
    public void step1_checkSomething() {        
        //do something
    }

    @Test(priority = 2)
    public void step2_clickOnSomething() {          
        //click on something            
    }

    @AfterTest
    public void initializeDriver(){
    objTND.QuitDriver();  
    }
}

If you want to use browser(to open) before each @Test, You can use this same method with @BeforeMethod annotation. 如果要在每个@Test之前使用浏览器(打开),则可以使用带有@BeforeMethod批注的相同方法。

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

相关问题 如何在Java中初始化依赖于彼此的类? - How to initialize classes dependent on each other, in Java? 如何初始化String originalHandle = driver.getWindowHandle(); 一旦? - How to initialize String originalHandle = driver.getWindowHandle(); once? 如果我们定义了 WebDriver 驱动程序,则在使用 TestNG 时; 全局然后不创建该类的对象我们如何能够在方法下使用驱动程序? - While using TestNG if we define WebDriver driver; globally then without creating object of that class how we are able to use driver under a method? 如何初始化驱动程序对象以便所有类都可以使用它 - How to initialize a driver object so it can be used by all classes WebDriver启动几个驱动程序实例 - WebDriver start several driver instances 如何在Java中的其他类中使用在公共类中定义的变量? - How to use variables defined in a public class in other classes in java? 如何在其他类别中使用变量 - How to use variables in other classes 如何一次将Java类中的所有变量初始化为零 - How can is initialize all variables in a Java class at once to zero 如何在所有测试类开始之前执行一段代码? - How to execute a piece of code once before all test classes start? 如何使用不同类别的同一个Webdriver - How to use the same Webdriver from different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM