1

I have a registration form that register many users ,the problem in the first loop when I click on create it go too fast and didn't register the first one and resister the second ..., so I use Thread.sleep(500); I want to avoid using sleep is there a way to do it

here is my code

 @Given("user on registration page and create users")
    public void user_on_registration_page_and_create_users() throws InterruptedException {

        System.out.println(userLoginPageDataList);

        for(UserLoginPageData userLoginPageData:userLoginPageDataList){
            userRegistrationPage.init();
            logger.info("*************************************** init the driver && go to registration page http://localhost:4200/register");

            logger.info("*************************************** reading line "+userLoginPageData.getRowIndex() +" from Excel file");
            userRegistrationPage.enterUserLogin(userLoginPageData.getUsername());
            userRegistrationPage.enterUserPassword(userLoginPageData.getPassword());
            userRegistrationPage.enterUserRole(userLoginPageData.getUserRole());
            userRegistrationPage.clickOnCreate();
         //   Thread.sleep(500);

            logger.info(userLoginPageData.getUsername()+" is registred");

        }
    }

2 Answers2

2

You can use explicit(smart) wait.

WebDriverWait w = new WebDriverWait(driver, 5); //will wait 5 seconds most , but if element is visuble in the third second it will wait 3 sec.
w.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit_btn")));

read more on When to use explicit wait vs implicit wait in Selenium Webdriver?

Gaj Julije
  • 1,538
  • 15
  • 32
  • 1
    I am using page Factory and I use the same wait.until(ExpectedConditions.elementToBeClickable(By.id("create"))).click(); – ZakaRia Janah Feb 02 '21 at 10:45
  • You have so many different types of ExpectedConditions, use what is the most suitable. In my answer I have trid just to intraduce you with explicit waits. – Gaj Julije Feb 02 '21 at 10:47
  • Yes , the problem that I have is after clicking – ZakaRia Janah Feb 02 '21 at 10:49
  • Ok, so after you click userRegistrationPage.clickOnCreate(); you face isse.Can you describe in details, what is the issue after that click, and what you expect, and wat is actual behavior. – Gaj Julije Feb 02 '21 at 10:52
  • OK, I have excel file , I loop through it and resister users ,in the first Test time lunching the test the first line not register and the other registered, runing test for second time it register all lines ,but when using Thread it register all line in the first Test – ZakaRia Janah Feb 02 '21 at 10:57
  • Your user from the first excell is not registrated, and all other are? And if you use the thread.sleep everything is fine. Did I understand you properly? – Gaj Julije Feb 02 '21 at 11:01
  • without Thread sleep just the first line in the first test when i re-run test it resgister them all – ZakaRia Janah Feb 02 '21 at 11:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228150/discussion-between-gaj-julije-and-zakaria-janah). – Gaj Julije Feb 02 '21 at 11:04
0

One of the possible solutions (when you work with PageFactory) is to implement your own Locator that can be extended from AjaxElementLocator.

Say you have a form and the form has some noticeable property saying that it is ready to accept the input (this might be some button state or displaying some label, etc).

So you can initialize your page object in the way its fields will be "available" if that condition is met.

This can be achieved using your custom Locator/LocatorFactory in your PageFactory.init().

For example here is the form of two fields. The condition saying it is ready for interaction is then create button is enabled:

class MyForm {

    @FindBy(id = "user")
    WebElement user;

    @FindBy(id = "create")
    WebElement create;

    public MyForm(SearchContext searchContext){
        PageFactory.initElements(field -> new AjaxElementLocator(searchContext, field, 10){
            @Override
            protected boolean isElementUsable(WebElement element) {
                return create.isEnabled();
            }
        }, this);
    }

}

Unless create button is enabled any attempt to invoke fields methods would be failing and the script will fail in 10 seconds of retries.

More details about how you use the conditions with page objects you can find in this post.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27