简体   繁体   English

如何在java中使用TestNG Selenium webdriver在多个类中使用相同的浏览器窗口?

[英]How to use the same browser window in multiple classes using TestNG Selenium webdriver in java?

I am trying to automate a webpage which has a login and post login has many menu item. 我正在尝试自动化一个有登录和登录后有很多菜单项的网页。 I would like to automate it in such a way that it logs into the webpage only once and then use the different menu items. 我想以这样的方式自动化它,它只登录网页一次,然后使用不同的菜单项。 Each new menu item automation is created in a different class. 每个新菜单项自动化都在不同的类中创建。

package pack1;

public class Init {

    public WebDriver driver;

    ChromeOptions options;

    @BeforeSuite
    public void beforeSuite() throws AWTException, InterruptedException, IOException {
        //Setting Chrome Driver and disabling the save password option
        System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\user\\Desktop\\Demo\\chromedriver.exe”);
        options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put(“credentials_enable_service”, false);
        prefs.put(“profile.password_manager_enabled”, false);
        options.setExperimentalOption(“prefs”, prefs);
        driver=new ChromeDriver(options);

        //Opening the URL
        driver.get(“myURL”);
        driver.manage().window().maximize();

        //Login to the portal
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[1]/input”)).sendKeys(username);
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[2]/input”)).sendKeys(password);
        driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[3]/button”)).click();

    }

    @AfterSuite
    public void afterSuite() {
        //Closing the driver
        // driver.close();
    }
}

Class A A级

    package pack1;

public class ClassA extends Init{

    @Test (priority=0, enabled = true)
    public void Setup() throws InterruptedException{
        //Traversing the menu to reach contract grower setup
        Thread.sleep(5000);
        driver.findElement(By.linkText(“Menu1”)).click();
        driver.findElement(By.linkText(“SubMenu1”)).click();

    }

}

Class B B级

    package pack1;

public class ClassBextends Init{

    @Test (priority=0, enabled = true)
    public void Setup() throws InterruptedException{
        //Traversing the menu to reach contract grower setup
        Thread.sleep(5000);
        driver.findElement(By.linkText(“Menu2”)).click();
        driver.findElement(By.linkText(“SubMenu2”)).click();
    }

}

testing.xml testing.xml

 <?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=”Suite”>
    <test name=”Test”>
        <classes>
            <class name=”pack1.ClassA”/>
            <class name=”pack1.ClassB”/>
            <class name=”pack1.Init”/>
        </classes>
    </test> <!– Test –>
</suite> <!– Suite –>

You should make the following changes: 您应该进行以下更改:

  • Configure WebDriver in the Init Class to be static Init Class中的WebDriver配置为static
  • Don't inherit the Init class in the Test Classes 不要在Test Classes中继承Init
  • To use driver in test classes, access it as Init.getDriver(); 要在测试类中使用driver ,请将其作为Init.getDriver();

Base Class 基类

public class Init {

    private static WebDriver driver;

    public static WebDriver getDriver() {
        return driver;
    }

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("BS");
        System.setProperty("webdriver.chrome.driver", "");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("AS");
        driver.quit();
    }
}

Class A A级

public class ClassA {
    @Test(priority = 0, enabled = true)
    public void classATest() throws InterruptedException {
        System.out.println("classATest");
        Init.getDriver().findElement(By.name("q")).sendKeys("Class 1");
    }
}

Class B B级

public class ClassB {
    @Test(priority = 0, enabled = true)
    public void class2Test() throws InterruptedException {
        System.out.println("classBTest");
        Init.getDriver().findElement(By.name("q")).sendKeys("Class 2");
    }
}

TestNG XML File TestNG XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="System Testing" parallel="none" thread-count="1">
    <test name="MenuTest" verbose="0">
        <classes>
            <class name="com.pack1.ClassA" />
            <class name="com.pack1.ClassB" />
            <class name="com.pack1.Init" />
        </classes>
    </test>
</suite> 

Output 产量

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
BS
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 16311
Only local connections are allowed.
Mar 09, 2018 2:33:59 PM org.openqa.selenium.remote.ProtocolHandshake.createSession
INFO: Detected dialect: OSS
classATest
classBTest
AS
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.541 sec - in TestSuite

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

相关问题 使用testng在一个浏览器中跨多个类运行Java Selenium Webdriver测试 - Running java selenium webdriver tests across many classes in one browser using testng Java Selenium中多个类中Webdriver的相同实例 - Same instance of Webdriver in multiple classes in Java Selenium 使用相同的Firefox窗口在Selenium WebDriver(Java)中运行多个测试 - Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java) 如何使用Java关闭Selenium WebDriver中的子浏览器窗口 - How to close child browser window in Selenium WebDriver using Java 如何通过将Selenium WebDriver与Java一起在同一浏览器中打开新标签页? - How to open a new tab in the same browser by using Selenium WebDriver with Java? 如何使用带有Java的Selenium WebDriver多次单击同一按钮 - How to click on a same button multiple times using Selenium WebDriver with Java Selenium Webdriver,使用Java的testNG - Selenium webdriver , testNG with java Selenium WebDriver,带有Java的TestNG - Selenium WebDriver, TestNG with Java 使用 selenium webdriver 和 java testng 中的数据提供程序 - data providers in testng using selenium webdriver and java 当浏览器窗口未聚焦时,使用Java的Selenium2(WebDriver)失败 - Selenium2 (WebDriver) using Java fails when browser window is not focused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM