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

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

Introduction

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

Prototype

public static ExpectedCondition<Boolean> attributeToBe(final WebElement element, final String attribute,
        final String value) 

Source Link

Document

An expectation for checking given WebElement has attribute with a specific value

Usage

From source file:com.fullteaching.backend.e2e.FullTeachingTestE2E.java

License:Apache License

private boolean checkVideoPlaying(BrowserUser user, WebElement videoElement, String containerQuerySelector) {

    // Video element should be in 'readyState'='HAVE_ENOUGH_DATA'
    user.getWaiter().until(ExpectedConditions.attributeToBe(videoElement, "readyState", "4"));

    // Video should have a valid 'src' value
    user.getWaiter().until(ExpectedConditions.attributeToBeNotEmpty(videoElement, "src"));

    // Video should have a srcObject (type MediaStream) with the attribute 'active' to true
    Assert.assertTrue((boolean) user.runJavascript("return document.querySelector('" + containerQuerySelector
            + "').getElementsByTagName('video')[0].srcObject.active"));

    // Video should trigger 'playing' event
    user.runJavascript("document.querySelector('" + containerQuerySelector
            + "').getElementsByTagName('video')[0].addEventListener('playing', window.MY_FUNC('"
            + containerQuerySelector + "'));");

    user.getWaiter().until(//from   ww w  .j  a va2 s .  com
            ExpectedConditions.textToBePresentInElementLocated(By.id("video-playing-div"), "VIDEO PLAYING"));
    user.runJavascript("document.body.removeChild(document.getElementById('video-playing-div'))");

    return true;
}

From source file:com.fullteaching.backend.e2e.FullTeachingTestE2ESleep.java

License:Apache License

private boolean checkVideoPlaying(BrowserUser user, WebElement videoElement, String containerQuerySelector) {

    // Video element should be in 'readyState'='HAVE_ENOUGH_DATA'
    user.getWaiter().until(ExpectedConditions.attributeToBe(videoElement, "readyState", "4"));

    // Video should have a srcObject (type MediaStream) with the attribute 'active' to true
    Assert.assertTrue((boolean) user.runJavascript("return document.querySelector('" + containerQuerySelector
            + "').getElementsByTagName('video')[0].srcObject.active"));

    // Video should trigger 'playing' event
    user.runJavascript("document.querySelector('" + containerQuerySelector
            + "').getElementsByTagName('video')[0].addEventListener('playing', window.MY_FUNC('"
            + containerQuerySelector + "'));");

    user.getWaiter().until(/* ww w  .  j a va 2  s  .c  o m*/
            ExpectedConditions.attributeContains(By.id("video-playing-div"), "innerHTML", "VIDEO PLAYING"));

    user.runJavascript("document.body.removeChild(document.getElementById('video-playing-div'))");

    return true;
}

From source file:com.liferay.faces.portal.test.integration.demo.JSFLoginPortletTester.java

License:Open Source License

public void testJSF_Login(boolean testRememberMe) {

    // 1. Navigate the browser to the portal page that contains the jsf-sign-in portlet.
    BrowserDriver browserDriver = getBrowserDriver();
    WaitingAsserter waitingAsserter = getWaitingAsserter();
    String jsfLoginPageURL = PortalTestUtil.getGuestPageURL("jsf-sign-in");
    browserDriver.navigateWindowTo(jsfLoginPageURL);

    String emailFieldXpath = "//input[contains(@id,':handle')]";
    browserDriver.waitForElementEnabled(emailFieldXpath);

    if (testRememberMe) {
        browserDriver.clickElement("//div[contains(@id,'rememberMe')]/input");
    }//from   w ww . j  a v  a  2  s.  co m

    // 2. Wait for the *Email Address* field element to accept input.
    browserDriver.waitForElementDisplayed(emailFieldXpath);
    waitingAsserter.assertElementDisplayed(emailFieldXpath);

    // 3. Clear the *Email Address* field.
    browserDriver.clearElement(emailFieldXpath);

    // 4. Enter "test@liferay.com" into the *Email Address* field.
    browserDriver.sendKeysToElement(emailFieldXpath, "test@liferay.com");

    String passwordFieldXpath = "//input[contains(@id,':password')]";
    String signInButtonXpath = "//input[@type='submit' and @value='Sign In']";

    // Note: skip steps 5-9 if you are testing the "Remember Me" feature.
    if (!testRememberMe) {

        // 5. Enter "invalid_password" into the *Password* feild.
        browserDriver.sendKeysToElement(passwordFieldXpath, "invalid_password");

        // 6. Click the *Sign In* button.
        browserDriver.clickElementAndWaitForRerender(signInButtonXpath);

        // 7. Verify that the "Authentication failed" error message is displayed.
        String messageErrorXpath = "//form[@method='post']/ul/li";
        waitingAsserter.assertTextPresentInElement("Authentication failed", messageErrorXpath);

        // 8. Verify that the email field value still contains "test@liferay.com".
        waitingAsserter.assertTextPresentInElementValue("test@liferay.com", emailFieldXpath);

        // 9. Verify that the password field value is empty.
        ExpectedCondition<Boolean> passwordValueEmptyCondition = ExpectedConditions
                .attributeToBe(By.xpath(passwordFieldXpath), "value", "");
        waitingAsserter.assertTrue(passwordValueEmptyCondition);
    }

    // 10. Enter "test" into the *Password* feild.
    browserDriver.sendKeysToElement(passwordFieldXpath, "test");

    // 11. Click the *Sign In* button.
    browserDriver.clickElement(signInButtonXpath);

    // 12. Verify that the 'Sign In' was successful.
    waitingAsserter.assertTextPresentInElement("You are signed in",
            "//div[contains(@class,'liferay-faces-bridge-body')]");

    //J-
    // 13. Close the browser.
    // 14. Reopen the browser.
    //J+

    // TECHNICAL NOTE: BrowserDriver removes all cookies on closing, so simulate the browser closing by removing all
    // cookies that do not have an expiry (non-persistent cookies). For more details, see here:
    // https://stackoverflow.com/questions/3869821/how-do-i-create-a-persistent-vs-a-non-persistent-cookie.
    Set<Cookie> browserCookies = browserDriver.getBrowserCookies();
    WebDriver webDriver = browserDriver.getWebDriver();

    for (Cookie cookie : browserCookies) {

        if (cookie.getExpiry() == null) {
            webDriver.manage().deleteCookie(cookie);
        }
    }

    // 15. Navigate the browser to the portal page that contains the jsf-sign-in portlet.
    browserDriver.navigateWindowTo(jsfLoginPageURL);

    // 16. If you are testing the "Remember Me" feature then,...
    if (testRememberMe) {

        // ...verify that you are still signed in.
        waitingAsserter.assertTextPresentInElement("You are signed in",
                "//div[contains(@class,'liferay-faces-bridge-body')]");
    }

    // Otherwise,...
    else {

        // ...verify that you are no longer signed in.
        waitingAsserter = getWaitingAsserter();
        waitingAsserter.assertTextNotPresentInElement("You are signed in",
                "//div[contains(@class,'liferay-faces-bridge-body')]", false);
    }
}

From source file:com.liferay.faces.test.alloy.issue.FACES_3274Tester.java

License:Open Source License

private static void assertAutocompleteAttributeOff(WaitingAsserter waitingAsserter, String inputXpath) {
    waitingAsserter.assertTrue(ExpectedConditions.attributeToBe(By.xpath(inputXpath), "autocomplete", "off"));
}

From source file:de.knowwe.uitest.UITestUtils.java

License:Open Source License

public static void awaitStatusChange(WebDriver driver, String status) {
    new WebDriverWait(driver, 10).until(ExpectedConditions
            .not(ExpectedConditions.attributeToBe(By.cssSelector("#knowWEInfoStatus"), "value", status)));
}

From source file:io.ddavison.conductor.Locomotive.java

License:Open Source License

public Locomotive setText(By by, String text) {
    waitForCondition(ExpectedConditions.not(ExpectedConditions.invisibilityOfElementLocated(by)))
            .waitForCondition(ExpectedConditions.elementToBeClickable(by));
    WebElement element = waitForElement(by);
    element.clear();//from w  w w  . ja  va2 s. c o m
    element.sendKeys(text);
    waitForCondition(ExpectedConditions.or(ExpectedConditions.textToBe(by, text),
            ExpectedConditions.attributeToBe(by, "value", text)));
    return this;
}

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

License:Apache License

@Test
@DisplayName("Remote composed record")
void remoteComposedRecordTest() throws Exception {
    isRecordingTest = true;/*from w  w w .j a v a2s  .  c  om*/

    setupBrowser("chrome");

    log.info("Remote composed record");

    final String sessionName = "COMPOSED_RECORDED_SESSION";
    final String resolution = "1280x720";

    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.id("session-name-input-0")).clear();
    user.getDriver().findElement(By.id("session-name-input-0")).sendKeys(sessionName);

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

    // Try to record a non-existing session
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

    listEmptyRecordings();

    // Try to stop a non-existing recording
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys("FAIL");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

    listEmptyRecordings();

    // Try to get a non-existing recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

    listEmptyRecordings();

    // Try to delete a non-existing recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [404]"));

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

    // Join the user to the session
    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));

    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);
    user.getDriver().findElement(By.id("rec-properties-btn")).click();
    Thread.sleep(500);
    WebElement resolutionField = user.getDriver().findElement(By.id("recording-resolution-field"));
    resolutionField.clear();
    resolutionField.sendKeys(resolution);

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

    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + sessionName + "]"));

    user.getEventManager().waitUntilEventReaches("recordingStarted", 1);

    Thread.sleep(5000);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(sessionName);

    // Try to start an ongoing recording
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [409]"));

    // Try to get an existing recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording got [" + sessionName + "]"));

    // Try to delete an ongoing recording
    user.getDriver().findElement(By.id("delete-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [409]"));

    // List existing recordings (one)
    user.getDriver().findElement(By.id("list-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording list [" + sessionName + "]"));

    // Stop ongoing recording
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + sessionName + "]"));

    user.getEventManager().waitUntilEventReaches("recordingStopped", 1);

    String recordingsPath = "/opt/openvidu/recordings/";
    File file1 = new File(recordingsPath + sessionName + "/" + sessionName + ".mp4");
    File file2 = new File(recordingsPath + sessionName + "/" + ".recording." + sessionName);
    File file3 = new File(recordingsPath + sessionName + "/" + sessionName + ".jpg");

    Assert.assertTrue("File " + file1.getAbsolutePath() + " does not exist or is empty",
            file1.exists() && file1.length() > 0);
    Assert.assertTrue("File " + file2.getAbsolutePath() + " does not exist or is empty",
            file2.exists() && file2.length() > 0);
    Assert.assertTrue("File " + file3.getAbsolutePath() + " does not exist or is empty",
            file3.exists() && file3.length() > 0);

    Assert.assertTrue("Recorded file " + file1.getAbsolutePath() + " is not fine", this.recordedFileFine(file1,
            new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(sessionName)));
    Assert.assertTrue("Thumbnail " + file3.getAbsolutePath() + " is not fine", this.thumbnailIsFine(file3));

    // Try to get the stopped recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording got [" + sessionName + "]"));

    // Try to list the stopped recording
    user.getDriver().findElement(By.id("list-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording list [" + sessionName + "]"));

    // Delete the recording
    user.getDriver().findElement(By.id("delete-recording-btn")).click();
    user.getWaiter().until(
            ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Recording deleted"));

    Assert.assertFalse("File " + file1.getAbsolutePath() + " shouldn't exist", file1.exists());
    Assert.assertFalse("File " + file2.getAbsolutePath() + " shouldn't exist", file2.exists());
    Assert.assertFalse("File " + file3.getAbsolutePath() + " shouldn't exist", file3.exists());

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

    gracefullyLeaveParticipants(1);
}

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

License:Apache License

@Test
@DisplayName("Remote individual record")
void remoteIndividualRecordTest() throws Exception {
    isRecordingTest = true;//from w  ww .  ja v a 2  s  . c  o  m

    setupBrowser("chrome");

    log.info("Remote individual record");

    final String sessionName = "TestSession";
    final String recordingName = "CUSTOM_NAME";

    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.id("session-name-input-0")).clear();
    user.getDriver().findElement(By.id("session-name-input-0")).sendKeys(sessionName);

    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));

    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);
    user.getDriver().findElement(By.id("rec-properties-btn")).click();
    Thread.sleep(500);

    // Set recording name
    user.getDriver().findElement(By.id("recording-name-field")).sendKeys(recordingName);
    // Set OutputMode to INDIVIDUAL
    user.getDriver().findElement(By.id("rec-outputmode-select")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("option-INDIVIDUAL")).click();
    Thread.sleep(500);

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

    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + sessionName + "]"));

    user.getEventManager().waitUntilEventReaches("recordingStarted", 2);

    Thread.sleep(5000);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(sessionName);

    // Try to start an ongoing recording
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [409]"));

    // Try to get an existing recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording got [" + sessionName + "]"));

    // Try to delete an ongoing recording
    user.getDriver().findElement(By.id("delete-recording-btn")).click();
    user.getWaiter()
            .until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Error [409]"));

    // List existing recordings (one)
    user.getDriver().findElement(By.id("list-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording list [" + sessionName + "]"));

    // Stop ongoing recording
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + sessionName + "]"));

    user.getEventManager().waitUntilEventReaches("recordingStopped", 2);

    String recordingsPath = "/opt/openvidu/recordings/";
    String recPath = recordingsPath + sessionName + "/";

    Recording recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(sessionName);
    this.checkIndividualRecording(recPath, recording, 2, "opus", "vp8", true);

    // Try to get the stopped recording
    user.getDriver().findElement(By.id("get-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording got [" + sessionName + "]"));

    // Try to list the stopped recording
    user.getDriver().findElement(By.id("list-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording list [" + sessionName + "]"));

    // Delete the recording
    user.getDriver().findElement(By.id("delete-recording-btn")).click();
    user.getWaiter().until(
            ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value", "Recording deleted"));

    Assert.assertFalse("Recording folder " + recPath + " shouldn't exist", new File(recPath).exists());

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

    gracefullyLeaveParticipants(2);
}

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

License:Apache License

@Test
@DisplayName("Remote record cross-browser audio-only and video-only")
void remoteRecordAudioOnlyVideoOnlyTest() throws Exception {
    isRecordingTest = true;/*  ww  w .  j a v  a 2 s  .  com*/

    setupBrowser("chromeAlternateScreenShare");

    log.info("Remote record cross-browser audio-only and video-only");

    final String SESSION_NAME = "TestSession";
    final String RECORDING_COMPOSED_VIDEO = "COMPOSED_VIDEO_ONLY";
    final String RECORDING_COMPOSED_AUDIO = "COMPOSED_AUDIO_ONLY";
    final String RECORDING_INDIVIDUAL_VIDEO = "INDIVIDUAL_VIDEO_ONLY";
    final String RECORDING_INDIVIDUAL_AUDIO = "INDIVIDUAL_AUDIO_ONLY";
    final int RECORDING_DURATION = 5000;

    Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread th, Throwable ex) {
            System.out.println("Uncaught exception: " + ex);
            synchronized (lock) {
                OpenViduTestAppE2eTest.ex = new Exception(ex);
            }
        }
    };

    Thread t = new Thread(() -> {
        MyUser user2 = new MyUser(new FirefoxUser("FirefoxUser", 30));
        otherUsers.add(user2);
        user2.getDriver().get(APP_URL);
        WebElement urlInput = user2.getDriver().findElement(By.id("openvidu-url"));
        urlInput.clear();
        urlInput.sendKeys(OPENVIDU_URL);
        WebElement secretInput = user2.getDriver().findElement(By.id("openvidu-secret"));
        secretInput.clear();
        secretInput.sendKeys(OPENVIDU_SECRET);

        user2.getEventManager().startPolling();

        // Firefox user audio + video
        user2.getDriver().findElement(By.id("add-user-btn")).click();

        // Firefox user video-only
        user2.getDriver().findElement(By.id("add-user-btn")).click();
        user2.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .send-audio-checkbox")).click();

        // Join Firefox users
        user2.getDriver().findElements(By.className("join-btn")).forEach(el -> el.sendKeys(Keys.ENTER));

        try {
            user2.getEventManager().waitUntilEventReaches("connectionCreated", 8);
            user2.getEventManager().waitUntilEventReaches("accessAllowed", 2);
            user2.getEventManager().waitUntilEventReaches("streamCreated", 8);
            user2.getEventManager().waitUntilEventReaches("streamPlaying", 8);

            int nVideos = user2.getDriver().findElements(By.tagName("video")).size();
            Assert.assertEquals("Expected 8 videos in Firefox user but found " + nVideos, 8, nVideos);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 2);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 2);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 4);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 4);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 6);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 6);

            user2.getEventManager().waitUntilEventReaches("recordingStarted", 8);
            user2.getEventManager().waitUntilEventReaches("recordingStopped", 8);

            user2.getEventManager().waitUntilEventReaches("streamDestroyed", 4);
            user2.getEventManager().waitUntilEventReaches("connectionDestroyed", 4);
            user2.getDriver().findElement(By.id("remove-user-btn")).click();
            user2.getDriver().findElement(By.id("remove-user-btn")).click();
            user2.getEventManager().waitUntilEventReaches("sessionDisconnected", 2);
        } catch (Exception e) {
            e.printStackTrace();
            user2.dispose();
            Thread.currentThread().interrupt();
        }
        user2.dispose();
    });
    t.setUncaughtExceptionHandler(h);
    t.start();

    // Chrome user screen share only-video
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-0 .screen-radio")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-0 .send-audio-checkbox")).click();

    // Chrome user audio-only
    user.getDriver().findElement(By.id("add-user-btn")).click();
    user.getDriver().findElement(By.cssSelector("#openvidu-instance-1 .send-video-checkbox")).click();

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

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

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

    user.getDriver().findElement(By.id("session-api-btn-0")).click();
    Thread.sleep(1000);
    user.getDriver().findElement(By.id("rec-properties-btn")).click();
    Thread.sleep(500);

    WebElement recordingNameField = user.getDriver().findElement(By.id("recording-name-field"));

    // Video-only COMPOSED recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_COMPOSED_VIDEO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 2);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME);
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 2);

    // Audio-only COMPOSED recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_COMPOSED_AUDIO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-1]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 4);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-1");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-1]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 4);

    // Video-only INDIVIDUAL recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_INDIVIDUAL_VIDEO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("rec-outputmode-select")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("option-INDIVIDUAL")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-2]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 6);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-2");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-2]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 6);

    // Audio-only INDIVIDUAL recording
    recordingNameField.clear();
    recordingNameField.sendKeys(RECORDING_INDIVIDUAL_AUDIO);
    user.getDriver().findElement(By.id("rec-hasaudio-checkbox")).click();
    user.getDriver().findElement(By.id("rec-hasvideo-checkbox")).click();
    Thread.sleep(500);
    user.getDriver().findElement(By.id("start-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording started [" + SESSION_NAME + "-3]"));
    user.getEventManager().waitUntilEventReaches("recordingStarted", 8);

    Thread.sleep(RECORDING_DURATION);

    user.getDriver().findElement(By.id("recording-id-field")).clear();
    user.getDriver().findElement(By.id("recording-id-field")).sendKeys(SESSION_NAME + "-3");
    user.getDriver().findElement(By.id("stop-recording-btn")).click();
    user.getWaiter().until(ExpectedConditions.attributeToBe(By.id("api-response-text-area"), "value",
            "Recording stopped [" + SESSION_NAME + "-3]"));
    user.getEventManager().waitUntilEventReaches("recordingStopped", 8);

    String recordingsPath = "/opt/openvidu/recordings/";

    // Check video-only COMPOSED recording
    String recPath = recordingsPath + SESSION_NAME + "/";
    Recording recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME);
    this.checkMultimediaFile(new File(recPath + recording.getName() + ".mp4"), false, true,
            recording.getDuration(), recording.getResolution(), null, "h264", true);

    // Check audio-only COMPOSED recording
    recPath = recordingsPath + SESSION_NAME + "-1/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-1");
    this.checkMultimediaFile(new File(recPath + recording.getName() + ".webm"), true, false,
            recording.getDuration(), null, "opus", null, true);

    // Check video-only INDIVIDUAL recording
    recPath = recordingsPath + SESSION_NAME + "-2/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-2");
    this.checkIndividualRecording(recPath, recording, 3, "opus", "vp8", true);

    // Check audio-only INDIVIDUAL recording
    recPath = recordingsPath + SESSION_NAME + "-3/";
    recording = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET).getRecording(SESSION_NAME + "-3");
    this.checkIndividualRecording(recPath, recording, 2, "opus", "vp8", true);

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

    gracefullyLeaveParticipants(2);

    t.join();

    synchronized (lock) {
        if (OpenViduTestAppE2eTest.ex != null) {
            throw OpenViduTestAppE2eTest.ex;
        }
    }
}

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);//from  w  w w.  java  2s  .com

    // 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);

}