1

I am working on a Selenium testing framework tutorial of John Sonmez and ran into problem trying to perform the very first test. The test part consists of two methods: 1. LoginPage.GoTo(); which opens wordpress login page and 2. LoginPage.LoginAs("admin").Login(); which inserts username and clicks continue. What happens when I run the test is wordpress loginpage opens and after 2 seconds blank Chrome browser opens and JUnit displays NoSuchElementException. The author of tutorial solved this problem adding some WebDriverWait and switchTo.ActiveElement.getAttribute() to LoginPage.GoTo(); method. However he is coding in C# and offers no solution for Java I'm coding in. I tried to apply WebDriverWait also but the problem is still there. Can anyone please suggest me how to solve this problem using WebDriverWait in Java.

LoginTest

import static org.junit.Assert.*;    
import org.junit.Assert;
import org.junit.Test;        
import wordpressAutomation.Driver;
import wordpressAutomation.LoginPage;

public class LoginTest extends Driver {

        @Test
        public void admin_user_can_login() {
            LoginPage l = new LoginPage();
            l.GoTo();
            LoginPage.LoginAs("scenicrail").Login();
            //Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
        }

}

LoginPage

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage extends Driver {

    @Test
    public void GoTo() {
        openBrowser().get("https://wordpress.com/log-in");
    }

    public static LoginCommand LoginAs(String username) {

        return new LoginCommand(username);
    }

}

public class LoginCommand extends Driver  {

    private String username;
    private String password; 

    public LoginCommand(String username) {

        this.username = username;

    }

    public LoginCommand WithPassword(String password) {

        this.password = password;
        return this;

    }


    public void Login() {

        WebElement login =  openBrowser().findElement(By.xpath("//*[@id='usernameOrEmail']"));
        login.sendKeys(username);
        openBrowser().findElement(By.xpath("//button[@type = 'submit']")).click();

    }   


}

public class Driver {

   public WebDriver openBrowser() {

      System.setProperty("webdriver.chrome.driver", "C:\\Users\\KatarinaOleg\\Desktop\\chromedriver_win32\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().window().maximize();
      //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      return driver;
   }    

}
0

2 Answers 2

2

After opening the page, where you get the NoSuchElementException you can add in something like this.

FluentWait<WebDriver> webDriverWait = new WebDriverWait(driver, 25).pollingEvery(5, TimeUnit.SECONDS);
webDriverWait.until(ExpectedConditions.visibilityOf(webElement));

Just add in whatever element you want to use as your check to make sure the page has loaded

Sign up to request clarification or add additional context in comments.

Comments

1

you have called the Openbrowser multiple times in Login Method from LoginCommand Class.So, It will be unnecessarily create different driver instance. So, your code needs to be modified as below along with explicit wait (Driver Class also needs to be changed as below)

Login Method from LoginCommand Class:

   public void Login() {

        WebDriverWait wait=new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='usernameOrEmail']")));
        WebElement login = driver.findElement(By.xpath("//*[@id='usernameOrEmail']"));

        login.sendKeys(username);
        driver.findElement(By.xpath("//button[@type = 'submit']")).click();
    }

Driver Class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Driver {

    WebDriver driver;

    public WebDriver openBrowser() {
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
}

Edit: (dmin_user_can_login() method in LoginTest class)

@Test
public void admin_user_can_login() {
    LoginPage l = new LoginPage();
    l.GoTo();

    l.LoginAs("scenicrail").Login();
    //Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
}

LoginPage Class:

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage extends Driver {

    @Test
    public void GoTo() {
        openBrowser().get("https://wordpress.com/log-in");
    }

    public LoginCommand LoginAs(String username) {

        return new LoginCommand(username);
    }

}

2 Comments

When I run my test first it asks me to make the field driver static, after I change it to static and run test it throws NullPointerException
you have to modify the LoginAs method as non-static and admin_user_can_login method should be accessed using the LoginPage class object (l). I have updated the answer with other method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.