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

时间:2018-12-04 19:08:13

标签: java selenium webdriver testng selenium-chromedriver

如果testng.xml中有10个类,它将打开10个浏览器会话。 如何初始化驱动程序一次,只打开一个测试用例的浏览器,然后关闭并再次打开第二个测试用例的窗口,依此类推?

我在构造函数中有驱动程序设置代码,这也许是一种不好的处理方式?如何在TNGDriver类中对其进行初始化,然后在Testcase1类中使用它?

我尝试使用@BeforeClass并有一个setUp方法,但这没用。

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类

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

<?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>

1 个答案:

答案 0 :(得分:0)

Chrome驱动程序默认情况下每次打开私有窗口时都会打开,您真的不需要此窗口。

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

您可以像下面这样构建结构:

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();
}

}

单元测试用例:

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();  
    }
}

如果要在每个@Test之前使用浏览器(打开),则可以使用带有@BeforeMethod批注的相同方法。