我需要登录才能开始测试。所以在我的测试类中,我使用@BeforeClass方法登录我的应用程序。然后我开始只是测试,但它没有工作。当我从@BeforeClass中删除我的登录并将其用于我的测试时,它工作正常。但在开始之前我不想登录我的所有测试。我认为有问题,我的对象是静态的,但我不知道为什么。 这是我的工作代码:
public class ModulesTests extends FunctionalTest {
static LoginPage loginPage;
static LoginPageReceipt loginPageReceipt;
@BeforeClass
public static void login() {
FunctionalTest.driver.get(GlobalVariables.URL);
loginPage = new LoginPage(FunctionalTest.driver);
loginPageReceipt = loginPage.login(GlobalVariables.USER_NAME, GlobalVariables.PASSWORD);
}
@Test
public void products() {
ProductsPage productsPage = loginPageReceipt.productsPage();
}
}
以下是它的工作原理:
public class ModulesTests extends FunctionalTest {
@BeforeClass
public static void login() {
FunctionalTest.driver.get(GlobalVariables.URL);
}
@Test
public void products() {
LoginPage loginPage = new LoginPage(FunctionalTest.driver);
LoginPageReceipt loginPageReceipt = loginPage.login(GlobalVariables.USER_NAME, GlobalVariables.PASSWORD);
ProductsPage productsPage = loginPageReceipt.productsPage();
}
}
我的其他课程:
public class FunctionalTest {
protected static WebDriver driver;
@BeforeClass
public static void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void cleanUp() {
driver.manage().deleteAllCookies();
}
@AfterClass
public static void tearDown() {
driver.close();
}
}
public class LoginPage extends PageObject {
@FindBy(id = "UserName")
private WebElement userName;
@FindBy(id = "Password")
private WebElement password;
@FindBy(id = "loginButton")
private WebElement loginButton;
public LoginPage(WebDriver driver) {
super(driver);
assertTrue(userName.isDisplayed());
assertTrue(password.isDisplayed());
assertTrue(loginButton.isDisplayed());
}
public void enterUserName(String userName) {
this.userName.clear();
this.userName.sendKeys(userName);
}
public void enterUserPassword(String password) {
this.password.clear();
this.password.sendKeys(password);
}
public LoginPageReceipt login(String userName, String password) {
enterUserName(userName);
enterUserPassword(password);
loginButton.click();
return new LoginPageReceipt(driver);
}
}
public class LoginPageReceipt extends PageObject {
public NavigationModules mainNav;
@FindBy(xpath = "//*[@id=\"loginPartial\"]")
private WebElement userNamePanel;
@FindBy(id = "products-menuitem")
private WebElement goToProductsPage;
public LoginPageReceipt(WebDriver driver) {
super(driver);
}
public String loginConfirmation() {
return userNamePanel.getText();
}
public ProductsPage productsPage() {
System.out.println(goToProductsPage.getText());
goToProductsPage.click();
return new ProductsPage(driver);
}
public NavigationModules initModuleNav() {
return new NavigationModules(driver);
}
}
public class ProductsPage extends PageObject {
@FindBy(id = "products-content-navigation")
private WebElement productMenu;
public ProductsPage(WebDriver driver) {
super(driver);
assertTrue(productMenu.isDisplayed());
}
}
public class PageObject {
protected WebDriver driver;
public PageObject(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
System.out.println(getClass().getName());
}
}
编辑。我刚看到当我使用@BeforeClass方法时,我也得到了java.lang.reflect.constructor.newinstance(未知来源)异常。
答案 0 :(得分:0)
您应该使用@Before
以便所有测试都必须在继续之前登录,同时,@BeforeClass
运行一次,并且它有用,以防多个测试将共享相同的对象。
请查看此link。