[英]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: 您应该进行以下更改:
WebDriver
in the Init
Class to be static
Init
Class中的WebDriver
配置为static
Init
class in the Test Classes Init
类 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.