package com.test;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.openqa.selenium.support.PageFactory;
import utility.BaseClass;
import com.pages.LoginPageObjects;
public class UnderTest extends BaseClass
{
@Test(description = "Email login")
public void f()
{
Reporter.log("===dude===", true);
LoginPageObjects lpage = PageFactory.initElements(driver, LoginPageObjects.class);
lpage.click_signin_link();
lpage.enter_uid("test@gmail.com");
lpage.click_submit();
}
@Test(description = "fb login")
public void fe() throws InterruptedException
{
Reporter.log("===dude===", true);
LoginPageObjects lpage = PageFactory.initElements(driver, LoginPageObjects.class);
lpage.click_fb_button();
Thread.sleep(5000);
}
}
如果我在两种方法中分别创建对象,测试运行良好。在@BeforeTest中声明它不起作用。如何减少该语句的重用?
答案 0 :(得分:1)
要访问LoginPageObjects
实例,您必须将其创建为class variable
。它适用于您班级中的所有方法。
然后,在@BeforeTest
注释中初始化此变量。
public class UnderTest extends BaseClass {
private LoginPageObjects lpage;
@BeforeTest
public void setUp() throws Exception {
lpage = PageFactory.initElements(driver, LoginPageObjects.class);
}
@Test(description = "Email login")
public void f()
{
Reporter.log("===dude===", true);
lpage.click_signin_link();
lpage.enter_uid("test@gmail.com");
lpage.click_submit();
}
@Test(description = "fb login")
public void fe() throws InterruptedException
{
Reporter.log("===dude===", true);
lpage.click_fb_button();
Thread.sleep(5000);
}
答案 1 :(得分:0)
尝试以下代码...
package com.test;
import org.testng.Reporter;
import org.testng.annotations.Test;
import org.openqa.selenium.support.PageFactory;
import utility.BaseClass;
import com.pages.LoginPageObjects;
public class UnderTest extends BaseClass
{
@Before
public void setUp() throws Exception {
LoginPageObjects lpage = PageFactory.initElements(driver,
LoginPageObjects.class);
}
@Test(description = "Email login")
public void f()
{
Reporter.log("===dude===", true);
lpage.click_signin_link();
lpage.enter_uid("test@gmail.com");
lpage.click_submit();
}
@Test(description = "fb login")
public void fe() throws InterruptedException
{
Reporter.log("===dude===", true);
lpage.click_fb_button();
Thread.sleep(5000);
}
}