2

Selenium newbie here... I am trying to create my first test framework .

Test Website : https://www.phptravels.net/

Test Case :

  1. Open Browser and enter the webpage
  2. Once the page is loaded , click on MyAccount ->Login

I have used xpath in my page object class and the script will run only till launching the webpage. It fails to click on the Login link .

I have tried to include an implicit wait assuming that the time taken for the page to load is longer than usual . Even then the issue persists.

Can you please help me understand what would be the correct xpath that this will work ?

Code :

POM_HomePage.java

package PageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class POM_HomePage {

    WebDriver driver;

    public POM_HomePage(WebDriver driver) {
        this.driver=driver;
        PageFactory.initElements(driver, this);
    }



    @FindBy(xpath="//*[@id='li_myaccount']/ul/li[1]/a")
    WebElement LinkMyAccount;
    public WebElement clickMyAccount() {
        return LinkMyAccount;
    }


}

HomePage.java

package TestGroupID;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

import PageObjects.POM_HomePage;
import Resources.MasterScript;


public class HomePage extends MasterScript{

    @Test
    public void SignIn() throws IOException {
        driver=LoadBrowser();
        LoadPropFile();
        driver.get(prop.getProperty("test_website"));
        POM_HomePage pomHome=new POM_HomePage(driver);
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        if (pomHome.clickMyAccount().isDisplayed()) {
            pomHome.clickMyAccount().click();
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        }
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
QQ_TT
  • 35
  • 4

1 Answers1

0

As per your question you have mentioned Once the page is loaded click on MyAccount ->Login. So you should have invoked click() method on two separate WebElements. But your POM_HomePage.java returns only one WebElement as @FindBy(xpath="//*[@id='li_myaccount']/ul/li[1]/a")

Solution

  • In POM_HomePage.java define two WebElements and two associated functions() as follows :

    • MyAccount Link

      @FindBy(xpath="//div[@class='navbar']//li[@id='li_myaccount']/a")
      WebElement LinkMyAccount;
      public WebElement clickMyAccount() {
          return LinkMyAccount;
      }
      
    • Login Link

      @FindBy(xpath="//div[@class='navbar']//li[@id='li_myaccount']//ul[@class='dropdown-menu']/li/a[contains(.,'Login')]")
      WebElement LinkLogin;
      public WebElement clickLogin() {
          return LinkLogin;
      }
      
  • In HomePage.java call isDisplayed() and click() for both the WebElements as follows :

    @Test
    public void SignIn() throws IOException {
        driver=LoadBrowser();
        LoadPropFile();
        driver.get(prop.getProperty("test_website"));
        POM_HomePage pomHome=new POM_HomePage(driver);
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        if (pomHome.clickMyAccount().isDisplayed()) {
                pomHome.clickMyAccount().click();
        }
        if (pomHome.clickLogin().isDisplayed()) {
            pomHome.clickLogin().click();
        }
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your solution. Unfortunately, the website seems to be having issues ( "our system folder path does not appear to be set correctly. Please open the following file and correct this: index.php" ) and have not been able to test it yet. – QQ_TT Feb 24 '18 at 15:00
  • 1
    Accepted the answer. Thanks again for your help. – QQ_TT Feb 24 '18 at 15:15