@FindBy(id = "registerUserLink")
private WebElement registerLink;
public UserRegistrationPage registerUser()
{
registerLink.click(); return PageFactory.initElements(driver, UserRegistrationPage.class);
}
@FindBy(id = "submit")
private WebElement submitRegistration;
public void verifyOnPage()
{
if (!submitRegistration.isDisplayed())
{
throw new IllegalStateException("This is not the registration page");
}
}
The issue then is the above sometimes throws the exception, because the page did not quite finish loading yet.
Here is the problem with the two work-arounds:
#1) Change implicit wait time
- This would be a near-perfect solution. By default the implicit wait is 0. I can up that to 10, 60, etc., and then the page starts working.
- However, this has detrimental impact on running the automated testing when you purposely are testing to see that a WebElement does not exist, etc. The test will then sit there until the full implicit wait time expires.
#2) Add an explicit wait in the verifyOnPage() method
- Yes, we can add a WebDriverWait with ExpectedConditions or a variant of this, it will work.
- However, the wait mechanism takes a By object and not a WebElement object. This means I cannot make any use of the awesome @FindBy WebElement I defined previously. It means I'd have to copy the By string into the explicit wait.
- This ends up with extra boilerplate code and defeating DRY
Unfortunately I don't really have a full solution to the dilemma. But to summarize what I believe to be ideal:
- Be able to make use of the WebElement object the PageFactory created for me so I don't have to "redo it" by using it's By string elsewhere in a WebDriverWait
- Possibly annotate the WebElement further to imply it's going to be used with an explicit wait with the option of specifying the time value
- This would also imply setting a default global explicit wait for the WebDriver instance
- Don't end up having to resort to the WebDriverWait derivatives, because for the most part it's boilerplate in this situation
The only problem with annotating the WebElement further with explicit wait meta-data is it then prevents it from also being used elsewhere with an implicit wait....but then again, the page should have already been loaded so that shouldn't be an issue!
Wait do you think? Any suggestions? Did I miss something that will solve this? I really want to get the most out of this pattern with the least amount of boilerplate! :)
Thanks.
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/QXBSfWpQUzMJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
@FindBy(id = "registerUserLink")
private WebElement registerLink;
vsBy registerLink = By.id("registerUserLink");
package pageobject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage extends Driver{
public LoginPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
@CacheLookup
@FindBy(name = "loginname")
public static WebElement usernameEditBox;
@CacheLookup
@FindBy(name = "password")
public static WebElement passwordEditBox;
@CacheLookup
@FindBy(name = "btnlogin")
public static WebElement submitBtn;
@CacheLookup
@FindBy(xpath = "//span[text()=' AppsOne']")
public static WebElement verifyLogoinpage;
public HomePage loginMethod(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);
}
public LoginPage invalidloginMethod(String username2, String password2) {
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(usernameEditBox)).sendKeys(username2);
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(passwordEditBox)).sendKeys(password2);
//driver.findElement(By.id("rememberMe")).click();
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOf(submitBtn)).click();
return this;
}
public String verifyLogo() {
String str =verifyLogoinpage.getText();
return str;
Hi,
I am curious to know doubts raised by Tony
Its been asked long back, i also have similar doubts
1. If i am using PF model, do i need to use explicit/fluent wait in my test cases. Dont PF automatically take care of all Web element.
2 In which cases we have to use AjaxElementLocatorFactory initialization.
If any one element in page coming through Ajax then do we need to use this?
3. In case i have to use explicit wait in my test cases, then how to get WebElement from a page.
case 1: All WebElements in Page are private, do i need to add getters for all WebElements present inside a Page.
@Test
public void test1(){
ExpectedConditions.elementToBeClickable(homePage.getWebElement())
}
case 2: In some explicit wait we need By locator to be passed, how to get By locator in case of PF pattern
@Test
public void test1(){
ExpectedConditions.visibilityOfElementLocated(RequireABylocator)
}
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+unsubscribe@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.