I need to be login in to start my tests. So in my test class I make @BeforeClass method where I login to my application. Then I start simply test but it did not work. When I remove my login from @BeforeClass and put it to my test it works fine. But I do not want to login in in all my test before I can start them. I think there is problem with that my object are static but i do not have clue why. Here is my not working code:
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();
}
}
Here is when it works:
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();
}
}
And rest of my classes:
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());
}
}
Edit. I just saw that when I use @BeforeClass method I get also java.lang.reflect.constructor.newinstance(unknown source) exception.