简体   繁体   中英

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

    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

    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

 <?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
  • Don't inherit the Init class in the Test Classes
  • To use driver in test classes, access it as 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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM