1

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.

slovvic
  • 417
  • 3
  • 6
  • 14
  • How many @BeforeClass do you have in the same class?. I mean login() and setup() method are in the same class? – Morvader Nov 10 '16 at 12:52
  • One, no login() is in LoginPage class and setup is in ModulesTest. I will edit those class. – slovvic Nov 10 '16 at 12:58
  • But maybe your suggestion is good. My test have BeforeClass method but class where I have this metod extends second class which also have BeforeClass method. – slovvic Nov 10 '16 at 13:06
  • Yep, I was going to tell you the same thing. Have to look to this answer http://stackoverflow.com/questions/2132734/beforeclass-and-inheritance-order-of-execution . Hope helps! – Morvader Nov 10 '16 at 13:07
  • I tested this and my BeforeClass methods are running in correct way. – slovvic Nov 10 '16 at 13:55
  • I think the problem is that you never create an instace of FunctionalTest. Is a public class but there is no new FunctionalTest anywhere. – Morvader Nov 10 '16 at 14:58
  • 1
    Maybe it is the reason. But i switch FunctionalTest to singleton pattern and remove BeforeClass and now all works fine :) – slovvic Nov 10 '16 at 15:28

1 Answers1

0

You should use @Before so that all your tests have to login before proceeding, meanwhile, @BeforeClass runs one time, and it's usefull just in case multiple tests will share same objects.

Please take a look on this link.

Community
  • 1
  • 1
Incepter
  • 2,711
  • 15
  • 33
  • But I do not want to login in in all my test before I can start them. I want to share my objects to another test. – slovvic Nov 10 '16 at 11:56
  • 1
    Does the login succeed? What is exactly the error you are getting ? – Incepter Nov 10 '16 at 12:00
  • Yes, something strange happens. I get logged out and NoSuchElementException what is obvious when i my logged out. – slovvic Nov 10 '16 at 12:04
  • One hint that i use to avoid all this in my case, i save a profil for the browser where i am logged in (so my credentials are stored in the browser profil), so i don't have to login at all. – Incepter Nov 10 '16 at 12:13
  • and i am sorry i couldn't know why this strange behaviour occurs. if the @Before can solve your problem, don't waste much time, or try changing the road you are taking! – Incepter Nov 10 '16 at 12:14