Selenium fluent wait and page factories

已查看 67 次
跳至第一个未读帖子

Niroshan Nad'jah

未读,
2018年4月17日 01:44:312018/4/17
收件人 Selenium Users

So, I've been rattling my head with this challenge, so far I'm not winning. I'd highly appreciate if anyone could assist me with this. Details as follows.

Below is the code Example: 1,

    import com.google.common.base.Function;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.FluentWait;
    import org.openqa.selenium.support.ui.Wait;
    import static java.util.concurrent.TimeUnit.MINUTES;
    import static java.util.concurrent.TimeUnit.SECONDS;

    public class Tester01 {

    public static void main(String[] args) {
    WebDriver webDriver = new ChromeDriver();
    webDriver.get("https://accounts.google.com/
      signin/v2/identifier? 
      continue=https%3A%2F%2Fmail.google.com%2Fmail%2F
      &service=mail&sacu=1&rip=1&flowName=GlifWebSignIn
      &flowEntry=ServiceLogin");

    webDriver.manage().window().maximize();
    WebElement webElement;

    Wait<WebDriver> fluentWaiter = new FluentWait<WebDriver> 
    (webDriver).withTimeout(1, MINUTES)
           .pollingEvery(5, SECONDS)
           .withMessage("element couldn't be found after 1 minutes")
           .ignoring(NoSuchElementException.class);


    webElement = fluentWaiter.until(new Function<WebDriver, 
    WebElement>()
    {
       public WebElement apply(WebDriver driver) {
          return driver.findElement(By.xpath
            ("//input[@id='identifierIdd']"));
        }
    });

    webElement.sendKeys("testemail.com");
    }
    }


In example 1, the incorrect xpath is this

"//input[@id='identifierIdd']"

This is deliberate just to examine fluent wait. As expected after 1 minute test exited and with exception org.openqa.selenium.NoSuchElementException

So, I've decided to experiment with fluentwait & pageFactory, unfortunately I couldn't figure out how to achieve the below line from Tester01 class with pageFactory.


return driver.findElement(By.xpath("//input[@id='identifierIdd']"));

  1. Details for fluentwait & pageFactory experimentation. Below is the code Example: 2,

import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class LocatorTest{

@FindBy(how = How.XPATH, using="//input[@id='identifierIdd']")
public WebElement elementTest;

}
And Tester01 class with LocatorTest class

    public class Tester01 {

    public static void main(String[] args) {
    WebDriver webDriver = new ChromeDriver();
    webDriver.get("https://accounts.google.com/signin/v2/identifier?                        
       continue=https%3A%2F%2Fmail.google.
       com%2Fmail%2F&service=mail&sacu=1&rip=1
       &flowName=GlifWebSignIn&flowEntry=ServiceLogin");

    webDriver.manage().window().maximize();
    WebElement webElement;

    Wait<WebDriver> fluentWaiter = new FluentWait<WebDriver>(webDriver)
            .withTimeout(1, MINUTES)
            .pollingEvery(5, SECONDS)
            .withMessage("element couldn't be found after 1 minutes")
            .ignoring(NoSuchElementException.class);

    LocatorTest locatorTest = new LocatorTest();
    PageFactory.initElements(webDriver, locatorTest);

    webElement = fluentWaiter.until
    (new Function<WebDriver, WebElement>()
    {
        public WebElement apply(WebDriver driver) {
            return locatorTest.elementTest;
        }
    });

    webElement.sendKeys("testemail.com");
        }

    }

So Tester01 class with pagefactory doesn't wait fluently for 1 min, test fails immediately with org.openqa.selenium.NoSuchElementException:

I think I know what the issue is however doesn't really know how to overcome this issue,

This is my explanation of the issue, in Example: 2 Tester01 class the return statement in fluentWaiter instance doesn't use the appropriate driver instance.

This is the line I'm talking about return locatorTest.elementTest; because I think the method apply() accepts an Webdriver instance however the line return locatorTest.elementTest;doesn't utilise the driver instance.

Is my thinking correct? could someone please assist me with this issue please? Or propose an alternative solution please?

Please let me know if any of the above doesn't make sense or need more info.

Thanks in advance Niro

anwesana kanungo

未读,
2018年4月17日 03:26:582018/4/17
收件人 Selenium Users
U can try something like this . I used this in my last project it worked for me . i used explict wait in page factory .

public LoginPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
 @FindBy(name = "login")
public static WebElement usernameEditBox;
    @FindBy(name = "pass")
    public static WebElement passwordEditBox;
    @FindBy(name = "btnlogin")
    public static WebElement submitBtn;
    @FindBy(xpath = "//span[text()=' Annn']")
    public static WebElement verifyLogoinpage;
    
   
public HomePage login(String username1, String password1) {
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(usernameEditBox)).sendKeys(username1);
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(passwordEditBox)).sendKeys(password1);
      new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(submitBtn)).click();

        return new HomePage(driver);
回复全部
回复作者
转发
0 个新帖子