0

I need to have following test:

  • open page (js loader is presented)
  • select box
  • delete box

When I do test without waiters:

    driver.get("http://example.com");
    mediaPage.selectBox("My");
    mediaPage.getDeleteButton().click();

It fails on second and third steps, because elements are not founded.

If I use Thread.sleep, everything works OK:

    driver.get("http://example.com");
    Thread.sleep(4000);
    mediaPage.selectBox("My");
    Thread.sleep(4000);
    mediaPage.getDeleteButton().click();

If I use Explicit Wait, it doesn't work (element is not founded):

    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.get("http://example.com");
    wait.until(ExpectedConditions.visibilityOf(mediaPage.getTitleOfPage()));
    mediaPage.selectBox("My");
    wait.until(ExpectedConditions.elementToBeClickable(mediaPage.getDeleteButton()));
    mediaPage.getDeleteButton().click();

Also, I tried to use Thread.sleep before select box without wait and use wait before delete box - it does not work, even for delete box (element is not founded):

    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.get("http://example.com");
    Thread.sleep(4000);
    mediaPage.selectBox("My");
    wait.until(ExpectedConditions.elementToBeClickable(mediaPage.getDeleteButton()));
    mediaPage.getDeleteButton().click();

If I use Fluent Wait for delete button, it works:

    driver.get("http://example.com");
    Thread.sleep(4000);
    mediaPage.selectBox("My");
           new FluentWait<>(driver).withTimeout(Duration.ofSeconds(5)).pollingEvery(Duration.ofMillis(500))
                    .ignoring(WebDriverException.class)
                    .until(d -> {
                        WebElement el = mediaPage.getDeleteButton();
                        el.click();
                        return el;
                    });

I don't understand, why Fluent Wait works, but Explicit doesn't?
Is it possible to use Explicit Wait instead of Fluent Wait?
If not, how to apply Fluent Wait to mediaPage.selectBox("My"); if it doesn't return anything at all?

UPDATED 1

MediaPage.java

    public class MediaPage {
        private WebDriver driver;
        public MediaPage(WebDriver driver) {
            this.driver = driver;
        }
        @FindBy(how = How.XPATH, using = "//div[@class='TitleText']")
        private WebElement titleOfPage;
        @FindBy(how = How.XPATH, using = "//div[@class='boxes']")
        private List<WebElement> boxes; 
        @FindBy(how = How.XPATH, using = "//div[contains(@class, 'deleteButton')]")
        private WebElement deleteButton;

        public void selectBox(String searchTitle) {
            if(searchTitle.equals("My")){
                boxes.get(1).click();
            } else{
                boxes.get(2).click();
            }
        }
        public WebElement getTitleOfPage() {
            return titleOfPage;
        }
        public WebElement getDeleteButton() {
            return deleteButton;
        }
    }

UiTestBase.java

    public class UiTestBase {
    
        public static WebDriver driver = null;
        public static String url;
    
        @BeforeTest()
        public void initialize() {
            url = "http://example.com";
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        }
    
        @AfterTest()
        public void tearDownTest() {
            UiTestBase.driver.quit();
        }
    }

MediaTest.java

    public class MediaTest extends UiTestBase {
        private WebDriverWait wait;
        private MediaPage mediaPage;
    
        @BeforeMethod()
        private void openLoginPage() {
    
            driver.manage().deleteAllCookies();
            wait = new WebDriverWait(driver, 10);
            mediaPage = PageFactory.initElements(driver, MediaPage.class);
            driver.get(url);
        }
    
        @Test()
        private void deleteBox() throws InterruptedException {
            
            Thread.sleep(4000);
            //wait.until(ExpectedConditions.elementToBeClickable(mediaPage.getTitleOfPage()));
            mediaPage.selectBox("My");
    
            Thread.sleep(3000);
            //wait.until(ExpectedConditions.elementToBeClickable(mediaPage.getDeleteButton()));
            mediaPage.getDeleteButton().click();
        }
    
        @AfterMethod()
        private void tearDown() {
        }
    }

UPDATED 2 Moreover here is the Test for uploading File, file could be uploaded in 1 second, but waiter would wait for 40 seconds to proceed with result.

UploadTest.java

    public class UploadTest extends UiTestBase {
    
    private WebDriverWait wait;
    private UploadPage uploadsPage;
    
    @BeforeMethod()
    private void openLoginPage() {
    driver.manage().deleteAllCookies();
    wait = new WebDriverWait(driver, 40);
    uploadsPage = PageFactory.initElements(driver, UploadsPage.class);
    driver.get(url);
    }
    
    @Test()
    private void uploadFile() {
    File randomMediaFile = new File("test.mp4");
    
    uploadsPage.getUploadInput().sendKeys(randomMediaFile.getAbsolutePath());
    
    try {
    wait.until(ExpectedConditions.invisibilityOf(uploadsPage.getUploadsProgress()));
    } 
    catch (Exception e) {
     System.out.println("Uploads finished");
    }
        }
    }

UploadPage.java

   public class UploadsPage {

    private WebDriver driver;

    public UploadsPage(WebDriver driver) {
        this.driver = driver;
    }

    @FindBy(how = How.XPATH, using = "//div[contains(@class, 'uploadsProgress')]")
    private WebElement uploadsProgress;

    @FindBy(how = How.XPATH, using = "//div[contains(@class, 'uploadInput')]")
    private WebElement uploadInput;

    public WebElement getUploadInput() {
        return uploadInput;
    }

    public WebElement getUploadsProgress() {
        return uploadsProgress;
    }
}
7
  • 1
    Show us your code for mediaPage.getTitleOfPage()
    – Alexey R.
    Mar 14 '19 at 16:24
  • @FindBy(how = How.XPATH, using = "//div[@class='TitleText']") private WebElement titleOfPage; public WebElement getTitleOfPage() { return titleOfPage; }
    – Artur
    Mar 14 '19 at 16:31
  • You're using page objects along with PageFactory - show your code (in the original post, please) of your page object, including the instantiation of the object Mar 14 '19 at 19:21
  • @BillHileman, done!
    – Artur
    Mar 14 '19 at 21:38
  • On which line does your code fail when you use ExplicitWait?
    – Alexey R.
    Mar 15 '19 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.