[英]Execute test method in junit4
Environment : Eclipse, Selenium-webdriver 2.31, Junit4. 环境:Eclipse,Selenium-webdriver 2.31,Junit 4。 I am modifying some scripts, n now when i execute this snippet the browser of chrome is started twice and thats obvious but am not sure how to start chrome only once and execute test method.
我正在修改一些脚本,现在执行该代码片段时,chrome的浏览器启动了两次,这很明显,但不确定如何仅启动chrome并执行测试方法。 Please correct me.
请纠正我。
Thats my LoginPage class, where the parameters are passed. 多数民众赞成在我的LoginPage类中,传递参数。 Here if i don't initialize WebDriver instance, then NPE Exception will be popped up.
如果我不初始化WebDriver实例,则将弹出NPE Exception。
Code : 代码:
@RunWith(Parameterized.class)
public class LoginPage{
WebDriver driver = new ChromeDriver();
String username;
String password;
public LoginPage(String username, String password)
{
this.username = username;
this.password = password;
}
@Test
public void loginAs(){
user.sendKeys(this.username);
pass.sendKeys(this.password);
}
} }
Now thats the Suite class, where suite classes are mentioned. 现在,多数民众赞成在套房类,其中提到套房类。 Link class is some another class.
链接类是另一类。
TestSuite Class Code : TestSuite类代码:
@RunWith(Suite.class)
@SuiteClasses({
LoginPage.class, Link.class
})
public class LoginTest{
static WebDriver driver;
@BeforeClass
public static void start()throws Exception
{
System.setProperty("webdriver.chrome.driver", "E:/Selenium/lib/chromedriver.exe");
driver = new ChromeDriver();
driver.get("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("Before Class");
}
@AfterClass
public static void teardown()
{
//
}
You are creating 2 chrome instances in your code: First when your @BeforeClass 您正在代码中创建2个chrome实例:首先是@BeforeClass
@BeforeClass
public static void start()throws Exception
{
System.setProperty("webdriver.chrome.driver", "E:/Selenium/lib/chromedriver.exe");
driver = new ChromeDriver();
And the again when you create LoginPage object. 当您创建LoginPage对象时,再次选择。
public class LoginPage{
WebDriver driver = new ChromeDriver();
So depending on your need, you have to remove one of these. 因此,根据您的需要,您必须删除其中之一。
I cant really see where the user and pass variables are assigned, but assuming they are webelements you probably get them from driver. 我真的看不到用户和传递变量的分配位置,但是假设它们是webelements,则可能是从驱动程序获得的。 So if you don't initiate driver in LoginPage you get nullpointer indeed.
因此,如果不在LoginPage中启动驱动程序,则确实会得到nullpointer。
If you still need to start the driver in your @BeforeClass and you want to use the same driver instance in your code, you could get a getter for this. 如果仍然需要在@BeforeClass中启动驱动程序,并且想在代码中使用相同的驱动程序实例,则可以使用此方法。
@RunWith(Suite.class)
@SuiteClasses({
LoginPage.class, Link.class
})
public class LoginTest{
static WebDriver driver;
public WebDriver getWebDriver(){
return driver; }
@BeforeClass
......
and you can call it in you constructor of login: 您可以在登录的构造函数中调用它:
WebDriver driver;
public LoginPage(String username, String password)
{
this.username = username;
this.password = password;
driver = LoginTest.getWebDriver();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.