Example usage for org.openqa.selenium.support.ui ExpectedConditions numberOfElementsToBe

List of usage examples for org.openqa.selenium.support.ui ExpectedConditions numberOfElementsToBe

Introduction

In this page you can find the example usage for org.openqa.selenium.support.ui ExpectedConditions numberOfElementsToBe.

Prototype

public static ExpectedCondition<List<WebElement>> numberOfElementsToBe(final By locator, final Integer number) 

Source Link

Document

An expectation for checking number of WebElements with given locator

Usage

From source file:com.example.selenium.Table.java

public boolean validateTable() {

    boolean flag = true;
    List<WebElement> tablesRows;
    try {//from ww  w . j  a v  a  2 s  . c  om

        Select selectRows = new Select(tableDropDown);
        tableDropDown.click();
        Thread.sleep(2000);

        selectRows.selectByValue("10");
        tableDropDown.click();
        WebDriverWait wait = new WebDriverWait(selenium, 10);
        tablesRows = wait.until(ExpectedConditions
                .numberOfElementsToBe(By.xpath("//table[@id='dataTables-example']/tbody/tr"), 10));
        if (tablesRows.size() != 10) {
            flag = false;
        }

        Thread.sleep(1000);
        selectRows.selectByValue("25");
        tableDropDown.click();
        tablesRows = wait.until(ExpectedConditions
                .numberOfElementsToBe(By.xpath("//table[@id='dataTables-example']/tbody/tr"), 25));
        if (tablesRows.size() != 25) {
            flag = false;
        }

        Thread.sleep(1000);
        selectRows.selectByValue("50");
        tableDropDown.click();
        tablesRows = wait.until(ExpectedConditions
                .numberOfElementsToBe(By.xpath("//table[@id='dataTables-example']/tbody/tr"), 50));
        if (tablesRows.size() != 50) {
            flag = false;
        }

        Thread.sleep(2000);
        tableSearch.sendKeys("Presto");
        Thread.sleep(1000);
        WebElement element = new WebDriverWait(selenium, 5).until(new ExpectedCondition<WebElement>() {
            public WebElement apply(WebDriver d) {
                return d.findElement(By.xpath("//td[contains(text(),'Presto')]"));
            }
        });
        if (!element.getText().equals("Presto")) {
            flag = false;
        }

        return flag;
    } catch (InterruptedException ex) {
        Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
    }
    return flag;
}

From source file:io.openvidu.test.e2e.OpenViduTestAppE2eTest.java

License:Apache License

@Test
@DisplayName("Moderator capabilities")
void moderatorCapabilitiesTest() throws Exception {

    setupBrowser("chrome");

    log.info("Moderator capabilities");

    // Add publisher
    user.getDriver().findElement(By.id("add-user-btn")).click();

    // Add subscriber
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .publish-checkbox")).click();

    // Add and configure moderator (only subscribe)
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-2 .publish-checkbox")).click();
    user.getDriver().findElement(By.id("session-settings-btn-2")).click();
    Thread.sleep(1000);/*  w ww  .  ja  v  a  2  s. co  m*/

    user.getDriver().findElement(By.id("radio-btn-mod")).click();
    user.getDriver().findElement(By.id("save-btn")).click();
    Thread.sleep(1000);

    user.getDriver().findElements(By.className("join-btn")).forEach(el -> el.sendKeys(Keys.ENTER));

    user.getEventManager().waitUntilEventReaches("connectionCreated", 9);
    user.getEventManager().waitUntilEventReaches("accessAllowed", 1);
    user.getEventManager().waitUntilEventReaches("streamCreated", 3);
    user.getEventManager().waitUntilEventReaches("streamPlaying", 3);

    int numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 3 videos but found " + numberOfVideos, 3, numberOfVideos);
    Assert.assertTrue("Videos were expected to have audio and video tracks", user.getEventManager()
            .assertMediaTracks(user.getDriver().findElements(By.tagName("video")), true, true));

    // Moderator forces unpublish
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-2 .force-unpub-btn")).click();
    user.getEventManager().waitUntilEventReaches("streamDestroyed", 3);

    List<WebElement> videos = user.getDriver().findElements(By.tagName("video"));
    numberOfVideos = videos.size();
    Assert.assertEquals("Expected 1 video but found " + numberOfVideos, 1, numberOfVideos);
    Assert.assertFalse("Publisher video should not have srcObject defined after force unpublish",
            user.getEventManager().hasMediaStream(videos.get(0), ""));

    // Publisher publishes again
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-0 .pub-btn")).click();
    user.getEventManager().waitUntilEventReaches("streamCreated", 6);
    user.getEventManager().waitUntilEventReaches("streamPlaying", 6);

    numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 3 videos but found " + numberOfVideos, 3, numberOfVideos);
    Assert.assertTrue("Videos were expected to have audio and video tracks", user.getEventManager()
            .assertMediaTracks(user.getDriver().findElements(By.tagName("video")), true, true));

    // Moderator forces disconnect of publisher
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-2 .force-disconnect-btn")).click();
    user.getEventManager().waitUntilEventReaches("streamDestroyed", 6);
    user.getEventManager().waitUntilEventReaches("connectionDestroyed", 2);
    user.getEventManager().waitUntilEventReaches("sessionDisconnected", 1);
    user.getWaiter().until(ExpectedConditions.numberOfElementsToBe(By.tagName("video"), 0));

    gracefullyLeaveParticipants(3);
}

From source file:io.openvidu.test.e2e.OpenViduTestAppE2eTest.java

License:Apache License

@Test
@DisplayName("Local record")
void localRecordTest() throws Exception {

    setupBrowser("chrome");

    log.info("Local record");

    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.className("join-btn")).click();

    user.getEventManager().waitUntilEventReaches("connectionCreated", 1);
    user.getEventManager().waitUntilEventReaches("accessAllowed", 1);
    user.getEventManager().waitUntilEventReaches("streamCreated", 1);
    user.getEventManager().waitUntilEventReaches("streamPlaying", 1);

    int numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 1 video but found " + numberOfVideos, 1, numberOfVideos);
    Assert.assertTrue("Video was expected to have audio and video tracks", user.getEventManager()
            .assertMediaTracks(user.getDriver().findElements(By.tagName("video")), true, true));

    WebElement recordBtn = user.getDriver()
            .findElement(By.cssSelector("#openvidu-instance-0 .publisher-rec-btn"));
    recordBtn.click();/*w  w  w .  j  av  a2 s.  c  o  m*/

    Thread.sleep(2000);

    WebElement pauseRecordBtn = user.getDriver()
            .findElement(By.cssSelector("#openvidu-instance-0 .publisher-rec-pause-btn"));
    pauseRecordBtn.click();

    Thread.sleep(2000);

    pauseRecordBtn.click();

    Thread.sleep(2000);

    recordBtn.click();

    user.getWaiter().until(ExpectedConditions.elementToBeClickable(By.cssSelector("#recorder-preview video")));

    user.getWaiter().until(
            waitForVideoDuration(user.getDriver().findElement(By.cssSelector("#recorder-preview video")), 4));

    user.getDriver().findElement(By.id("close-record-btn")).click();

    user.getWaiter()
            .until(ExpectedConditions.numberOfElementsToBe(By.cssSelector("#recorder-preview video"), 0));

    gracefullyLeaveParticipants(1);
}

From source file:io.openvidu.test.e2e.OpenViduTestAppE2eTest.java

License:Apache License

@Test
@DisplayName("REST API: Fetch all, fetch one, force disconnect, force unpublish, close session")
void restApiFetchForce() throws Exception {
    setupBrowser("chrome");

    log.info("REST API: Fetch all, fetch one, force disconnect, force unpublish, close session");

    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.id("add-user-btn")).click();

    // API REST test
    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);// w w w . java2s .c  o m

    // Close session (undefined)
    user.getDriver().findElement(By.id("close-session-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Error [Session undefined]"));

    // Fetch one (undefined)
    user.getDriver().findElement(By.id("get-session-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Error [Session undefined]"));

    // Fetch all (no active sessions)
    user.getDriver().findElement(By.id("list-sessions-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeContains(By.id("api-response-text-area"), "value",
            "Number: 0. Changes: false"));

    user.getDriver().findElement(By.id("close-dialog-btn")).click();
    Thread.sleep(1000);

    user.getDriver().findElement(By.id("auto-join-checkbox")).click();
    user.getDriver().findElement(By.id("one2one-btn")).click();

    user.getEventManager().waitUntilEventReaches("connectionCreated", 4);
    user.getEventManager().waitUntilEventReaches("accessAllowed", 2);
    user.getEventManager().waitUntilEventReaches("streamCreated", 4);
    user.getEventManager().waitUntilEventReaches("streamPlaying", 4);

    int numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 4 videos but found " + numberOfVideos, 4, numberOfVideos);
    Assert.assertTrue("Videos were expected to have audio and video tracks", user.getEventManager()
            .assertMediaTracks(user.getDriver().findElements(By.tagName("video")), true, true));

    // Fetch existing session (change)
    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);
    user.getDriver().findElement(By.id("get-session-btn")).click();

    Thread.sleep(1000);
    System.out.println(getBase64Screenshot(user));
    System.out.println(user.getDriver().findElement(By.id("api-response-text-area")).getAttribute("value"));

    user.getWaiter().until(
            ExpectedConditions.attributeContains(By.id("api-response-text-area"), "value", "Changes: true"));

    // Store connectionId and streamId
    String response = user.getDriver().findElement(By.id("api-response-text-area")).getAttribute("value");
    JSONObject json = (JSONObject) ((JSONArray) new JSONParser().parse(response.split("%")[1])).get(0);
    String connectionId = (String) json.keySet().iterator().next();
    String streamId = (String) ((JSONObject) ((JSONArray) json.get(connectionId)).get(0)).get("streamId");

    // Fetch all sessions (no change)
    user.getDriver().findElement(By.id("list-sessions-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeContains(By.id("api-response-text-area"), "value",
            "Number: 1. Changes: false"));

    // Force unpublish wrong
    user.getDriver().findElement(By.id("resource-id-field")).clear();
    user.getDriver().findElement(By.id("resource-id-field")).sendKeys("FAIL");
    user.getDriver().findElement(By.id("force-unpublish-api-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

    // Force unpublish right
    user.getDriver().findElement(By.id("resource-id-field")).clear();
    user.getDriver().findElement(By.id("resource-id-field")).sendKeys(streamId);
    user.getDriver().findElement(By.id("force-unpublish-api-btn")).click();
    user.getWaiter().until(
            ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Stream unpublished"));
    user.getEventManager().waitUntilEventReaches("streamDestroyed", 2);

    numberOfVideos = user.getDriver().findElements(By.tagName("video")).size();
    Assert.assertEquals("Expected 3 videos but found " + numberOfVideos, 3, numberOfVideos);

    // Force disconnect wrong
    user.getDriver().findElement(By.id("resource-id-field")).clear();
    user.getDriver().findElement(By.id("resource-id-field")).sendKeys("FAIL");
    user.getDriver().findElement(By.id("force-disconnect-api-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

    // Force disconnect right
    user.getDriver().findElement(By.id("resource-id-field")).clear();
    user.getDriver().findElement(By.id("resource-id-field")).sendKeys(connectionId);
    user.getDriver().findElement(By.id("force-disconnect-api-btn")).click();
    user.getWaiter().until(
            ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "User disconnected"));
    user.getEventManager().waitUntilEventReaches("connectionDestroyed", 1);

    // Close session
    user.getDriver().findElement(By.id("close-session-btn")).click();
    user.getWaiter().until(
            ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Session closed"));

    user.getWaiter().until(ExpectedConditions.numberOfElementsToBe(By.tagName("video"), 0));

    user.getDriver().findElement(By.id("close-dialog-btn")).click();
    Thread.sleep(500);

    gracefullyLeaveParticipants(1);

}

From source file:org.apache.archiva.web.test.RepositoryAdminTest.java

License:Apache License

@Test
public void testManagedRepository() {
    // login( getAdminUsername(), getAdminPassword() );
    WebDriverWait wait = new WebDriverWait(getWebDriver(), 20);
    WebElement el;//from   w w  w  . j  a v  a 2  s  .co  m
    el = wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-repositories-list-a")));
    tryClick(el, ExpectedConditions.presenceOfElementLocated(By.id("managed-repositories-view-a")),
            "Managed Repositories not activated");
    el = wait.until(
            ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='#remote-repositories-content']")));
    tryClick(el,
            ExpectedConditions.visibilityOfElementLocated(
                    By.xpath("//table[@id='remote-repositories-table']//td[contains(text(),'central')]")),
            "Remote Repositories View not available");
    el = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='#remote-repository-edit']")));
    el = tryClick(el, ExpectedConditions.visibilityOfElementLocated(By.id("remote-repository-save-button")),
            "Repository Save Button not available");

    setFieldValue("id", "myrepoid");
    setFieldValue("name", "My repo name");
    setFieldValue("url", "http://www.repo.org");

    el = wait.until(ExpectedConditions.elementToBeClickable(By.id("remote-repository-save-button")));
    Actions actions = new Actions(getWebDriver());
    actions.moveToElement(el);
    actions.perform();
    ((JavascriptExecutor) getWebDriver()).executeScript("arguments[0].scrollIntoView();", el);
    el.click();
    el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("remote-repositories-view-a")));
    ((JavascriptExecutor) getWebDriver()).executeScript("arguments[0].scrollIntoView();", el);
    tryClick(By.id("menu-proxy-connectors-list-a"),
            ExpectedConditions
                    .visibilityOfElementLocated(By.id("proxy-connectors-view-tabs-a-network-proxies-grid")),
            "Network proxies not available", 3, 10);
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("main-content"), "Proxy Connectors"));
    // proxy connect
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "central"));
    assertTextNotPresent("myrepoid");
    el = wait.until(ExpectedConditions.elementToBeClickable(By.id("proxy-connectors-view-tabs-a-edit")));
    el.click();
    el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proxy-connector-btn-save")));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("remote-repository-edit-fieldset")));
    // Another hack, don't know why the normal selectValue() does not work here
    ((JavascriptExecutor) getWebDriver()).executeScript("jQuery('#sourceRepoId').css('display','block')");
    Select select = new Select(getWebDriver().findElement(By.xpath(".//select[@id='sourceRepoId']")));
    select.selectByVisibleText("internal");
    // selectValue( "sourceRepoId", "internal", true );
    // Workaround
    // TODO: Check after upgrade of htmlunit, bootstrap or jquery
    // TODO: Check whats wrong here
    ((JavascriptExecutor) getWebDriver()).executeScript("$('#targetRepoId').show();");
    // End of Workaround
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("targetRepoId")));
    selectValue("targetRepoId", "myrepoid");
    el.click();
    wait.until(
            ExpectedConditions.textToBePresentInElementLocated(By.id("user-messages"), "ProxyConnector added"));
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "central"));
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "myrepoid"));

    tryClick(By.xpath("//i[contains(@class,'icon-resize-vertical')]//ancestor::a"),
            ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("proxy-connector-edit-order-div")),
            "Edit order view not visible", 3, 10);
    // clickLinkWithXPath( "//i[contains(@class,'icon-resize-vertical')]//ancestor::a");
    // This is needed here for HTMLUnit Tests. Currently do not know why, wait is not working for the
    // list entries down
    // waitPage();
    // el = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("proxy-connector-edit-order-div")));
    assertTextPresent("internal");
    List<WebElement> repos = wait.until(ExpectedConditions
            .numberOfElementsToBe(By.xpath("//div[@id='proxy-connector-edit-order-div']/div"), 2));
    Assert.assertTrue("First repo is myrepo", repos.get(0).getText().contains("myrepoid"));
    Assert.assertTrue("Second repo is central", repos.get(1).getText().contains("central"));

    // works until this point
    /*getSelenium().mouseDown( "xpath=//div[@id='proxy-connector-edit-order-div']/div[1]" );
    getSelenium().mouseMove( "xpath=//div[@id='proxy-connector-edit-order-div']/div[2]" );
    getSelenium().mouseUp( "xpath=//div[@id='proxy-connector-edit-order-div']/div[last()]" );
    Assert.assertTrue( "Second repo is myrepo", getSelenium().getText("xpath=//div[@id='proxy-connector-edit-order-div']/div[2]" ).contains( "myrepoid" ));
    Assert.assertTrue( "First repo is central", getSelenium().getText("xpath=//div[@id='proxy-connector-edit-order-div']/div[1]" ).contains( "central" ));
    */
}

From source file:org.eclipse.che.selenium.pageobject.MultiSplitPanel.java

License:Open Source License

/**
 * wait a number of the open split panels
 *
 * @param numberPanels is a number of open split panels
 *//*  w  w  w .j a v  a2 s .c o  m*/
public void waitNumberOpenSplitPanels(int numberPanels) {
    new WebDriverWait(seleniumWebDriver, REDRAW_UI_ELEMENTS_TIMEOUT_SEC)
            .until(ExpectedConditions.numberOfElementsToBe(By.id(Locators.TABS_PANEL_ID), numberPanels));
}

From source file:org.kie.wb.selenium.util.Waits.java

License:Apache License

public static void elementAbsent(By locator) {
    new WebDriverWait(GrapheneUtil.getDriver(), DEFAULT_TIMEOUT)
            .until(ExpectedConditions.numberOfElementsToBe(locator, 0));
}

From source file:Pages.litecartShop.CartPage.java

public void waitForCountProductsToBe(int count) {
    WebDriverWait wait = new WebDriverWait(driver, TestConstants.EXPLICIT_WAIT_SECONDS);
    //wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf(table)));
    wait.until(ExpectedConditions.numberOfElementsToBe(By.cssSelector("td.item"), count));
}