Example usage for org.openqa.selenium By name

List of usage examples for org.openqa.selenium By name

Introduction

In this page you can find the example usage for org.openqa.selenium By name.

Prototype

public static By name(String name) 

Source Link

Usage

From source file:com.chtr.tmoauto.webui.CommonFunctions.java

License:Open Source License

/**
 * /*w ww.  ja v a  2 s. c  o  m*/
 * @param locator
 * @return
 */
public By getSelector(String locator) {
    String[] prefix = locator.split("=", 2);
    if (prefix[0].equals("css")) {
        return By.cssSelector(prefix[1]);
    } else if (prefix[0].equals("id")) {
        return By.id(prefix[1]);
    } else if (prefix[0].equals("class")) {
        return By.className(prefix[1]);
    } else if (prefix[0].equals("xpath")) {
        return By.xpath(prefix[1]);
    } else if (prefix[0].equals("link")) {
        return By.linkText(prefix[1]);
    } else if (prefix[0].equals("name")) {
        return By.name(prefix[1]);
    } else if (locator.startsWith("//")) {
        return By.xpath(locator);
    } else if (locator.startsWith(".//")) {
        return By.xpath(locator);
    } else {
        return By.id(locator);
    }
}

From source file:com.coderoad.automation.rocketTruedx.RocketTruedxNaBasePage.java

License:Open Source License

public void openPage(String url) throws InterruptedException {

    super.openPage(url);
    Thread.sleep(2000);/*from www.j  a  v  a2 s .  c  om*/
    this.waitForVisibility(getDriver().findElement(By.name("userName")));
}

From source file:com.cognifide.qa.bb.aem.ui.wcm.windows.CreateSiteWindow.java

License:Apache License

/**
 * Fill data on Live copy view/*from  w w  w. j a  va2  s  .c om*/
 *
 * @param siteOwner site owner name
 * @param liveCopy live copy
 * @param rollOutConfigs list of roll out configurations
 * @return this CreateSiteWindow
 */
public CreateSiteWindow fillLiveCopy(String siteOwner, boolean liveCopy, List<String> rollOutConfigs) {
    ownerDropDown.selectByText(siteOwner);
    WebElement checkBox = currentWindow.findElement(By.name("isLiveCopy"));
    if (!liveCopy) {
        checkBox.click();
    }

    for (String config : rollOutConfigs) {
        currentWindow.findElement(By.xpath("//span[text()='Add Item']")).click();
        List<WebElement> inputs = currentWindow
                .findElements(By.xpath("//img[@src='/libs/cq/ui/resources/0.gif']"));
        inputs.get(inputs.size() - 1).click();
        List<WebElement> options = currentWindow.findElements(By.xpath("//div[text()='" + config + "']"));
        options.stream().filter(WebElement::isDisplayed).forEach(org.openqa.selenium.WebElement::click);
    }
    return this;
}

From source file:com.consol.citrus.dsl.design.SeleniumTestDesignerTest.java

License:Apache License

@Test
public void testSeleniumBuilder() {
    MockTestDesigner builder = new MockTestDesigner(applicationContext, context) {
        @Override//from  ww w  .j  a  v  a  2 s  . c  om
        public void configure() {
            selenium().start(seleniumBrowser);

            selenium().navigate("http://localhost:9090");

            selenium().find().element(By.id("target"));
            selenium().find().element("class-name", "${cssClass}").tagName("button").enabled(false)
                    .displayed(false).text("Click Me!").style("color", "red").attribute("type", "submit");

            selenium().click().element(By.linkText("Click Me!"));
            selenium().hover().element(By.linkText("Hover Me!"));

            selenium().setInput("Citrus").element(By.name("username"));
            selenium().checkInput(false).element(By.xpath("//input[@type='checkbox']"));

            selenium().javascript("alert('Hello!')").errors("This went wrong!");

            selenium().alert().text("Hello!").accept();

            selenium().clearCache();

            selenium().store("classpath:download/file.txt");
            selenium().getStored("file.txt");

            selenium().open().window("my_window");
            selenium().focus().window("my_window");
            selenium().close().window("my_window");

            selenium().waitUntil().hidden().element(By.name("hiddenButton"));

            selenium().stop();
        }
    };

    builder.configure();

    TestCase test = builder.getTestCase();
    int actionIndex = 0;
    Assert.assertEquals(test.getActionCount(), 18);

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StartBrowserAction.class);
    StartBrowserAction startBrowserAction = (StartBrowserAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(startBrowserAction.getName(), "selenium:start");
    Assert.assertNotNull(startBrowserAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            NavigateAction.class);
    NavigateAction navigateAction = (NavigateAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(navigateAction.getName(), "selenium:navigate");
    Assert.assertEquals(navigateAction.getPage(), "http://localhost:9090");
    Assert.assertNull(navigateAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            FindElementAction.class);
    FindElementAction findElementAction = (FindElementAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(findElementAction.getName(), "selenium:find");
    Assert.assertEquals(findElementAction.getBy(), By.id("target"));
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            FindElementAction.class);
    findElementAction = (FindElementAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(findElementAction.getName(), "selenium:find");
    Assert.assertEquals(findElementAction.getProperty(), "class-name");
    Assert.assertEquals(findElementAction.getPropertyValue(), "${cssClass}");
    Assert.assertEquals(findElementAction.getTagName(), "button");
    Assert.assertEquals(findElementAction.getText(), "Click Me!");
    Assert.assertFalse(findElementAction.isEnabled());
    Assert.assertFalse(findElementAction.isDisplayed());
    Assert.assertEquals(findElementAction.getStyles().size(), 1L);
    Assert.assertEquals(findElementAction.getStyles().get("color"), "red");
    Assert.assertEquals(findElementAction.getAttributes().size(), 1L);
    Assert.assertEquals(findElementAction.getAttributes().get("type"), "submit");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            ClickAction.class);
    ClickAction clickAction = (ClickAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(clickAction.getName(), "selenium:click");
    Assert.assertEquals(clickAction.getBy(), By.linkText("Click Me!"));
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            HoverAction.class);
    HoverAction hoverAction = (HoverAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(hoverAction.getName(), "selenium:hover");
    Assert.assertEquals(hoverAction.getBy(), By.linkText("Hover Me!"));
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            SetInputAction.class);
    SetInputAction setInputAction = (SetInputAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(setInputAction.getName(), "selenium:set-input");
    Assert.assertEquals(setInputAction.getBy(), By.name("username"));
    Assert.assertEquals(setInputAction.getValue(), "Citrus");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            CheckInputAction.class);
    CheckInputAction checkInputAction = (CheckInputAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(checkInputAction.getName(), "selenium:check-input");
    Assert.assertEquals(checkInputAction.getBy(), By.xpath("//input[@type='checkbox']"));
    Assert.assertFalse(checkInputAction.isChecked());
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            JavaScriptAction.class);
    JavaScriptAction javaScriptAction = (JavaScriptAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(javaScriptAction.getName(), "selenium:javascript");
    Assert.assertEquals(javaScriptAction.getScript(), "alert('Hello!')");
    Assert.assertEquals(javaScriptAction.getExpectedErrors().size(), 1L);
    Assert.assertEquals(javaScriptAction.getExpectedErrors().get(0), "This went wrong!");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            AlertAction.class);
    AlertAction alertAction = (AlertAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(alertAction.getName(), "selenium:alert");
    Assert.assertEquals(alertAction.getText(), "Hello!");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            ClearBrowserCacheAction.class);
    ClearBrowserCacheAction clearBrowserCacheAction = (ClearBrowserCacheAction) ((DelegatingTestAction) test
            .getActions().get(actionIndex++)).getDelegate();
    Assert.assertEquals(clearBrowserCacheAction.getName(), "selenium:clear-cache");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StoreFileAction.class);
    StoreFileAction storeFileAction = (StoreFileAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(storeFileAction.getName(), "selenium:store-file");
    Assert.assertEquals(storeFileAction.getFilePath(), "classpath:download/file.txt");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            GetStoredFileAction.class);
    GetStoredFileAction getStoredFileAction = (GetStoredFileAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(getStoredFileAction.getName(), "selenium:get-stored-file");
    Assert.assertEquals(getStoredFileAction.getFileName(), "file.txt");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            OpenWindowAction.class);
    OpenWindowAction openWindowAction = (OpenWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(openWindowAction.getName(), "selenium:open-window");
    Assert.assertEquals(openWindowAction.getWindowName(), "my_window");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            SwitchWindowAction.class);
    SwitchWindowAction switchWindowAction = (SwitchWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(switchWindowAction.getName(), "selenium:switch-window");
    Assert.assertEquals(switchWindowAction.getWindowName(), "my_window");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            CloseWindowAction.class);
    CloseWindowAction closeWindowAction = (CloseWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(closeWindowAction.getName(), "selenium:close-window");
    Assert.assertEquals(closeWindowAction.getWindowName(), "my_window");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            WaitUntilAction.class);
    WaitUntilAction waitUntilAction = (WaitUntilAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(waitUntilAction.getName(), "selenium:wait");
    Assert.assertEquals(waitUntilAction.getBy(), By.name("hiddenButton"));
    Assert.assertEquals(waitUntilAction.getCondition(), "hidden");
    Assert.assertNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StopBrowserAction.class);
    StopBrowserAction stopBrowserAction = (StopBrowserAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(stopBrowserAction.getName(), "selenium:stop");
    Assert.assertNull(stopBrowserAction.getBrowser());
}

From source file:com.consol.citrus.dsl.runner.SeleniumTestRunnerTest.java

License:Apache License

@Test
public void testSeleniumBuilder() {
    when(applicationContextMock.getBean(TestContext.class))
            .thenReturn(applicationContext.getBean(TestContext.class));
    when(applicationContextMock.getBean(TestActionListeners.class)).thenReturn(new TestActionListeners());
    when(applicationContextMock.getBeansOfType(SequenceBeforeTest.class))
            .thenReturn(new HashMap<String, SequenceBeforeTest>());
    when(applicationContextMock.getBeansOfType(SequenceAfterTest.class))
            .thenReturn(new HashMap<String, SequenceAfterTest>());

    when(seleniumBrowser.getEndpointConfiguration()).thenReturn(seleniumBrowserConfiguration);
    when(seleniumBrowserConfiguration.getBrowserType()).thenReturn(BrowserType.CHROME);
    when(seleniumBrowser.getWebDriver()).thenReturn(webDriver);

    when(seleniumBrowser.getName()).thenReturn("mockBrowser");
    when(applicationContextMock.getBean("mockBrowser", SeleniumBrowser.class)).thenReturn(seleniumBrowser);

    when(webDriver.navigate()).thenReturn(navigation);
    when(webDriver.manage()).thenReturn(options);
    when(webDriver.switchTo()).thenReturn(locator);
    when(locator.alert()).thenReturn(alert);
    when(alert.getText()).thenReturn("Hello!");

    when(webDriver.findElement(By.id("header"))).thenReturn(element);
    when(element.getTagName()).thenReturn("h1");
    when(element.isEnabled()).thenReturn(true);
    when(element.isDisplayed()).thenReturn(true);

    when(webDriver.findElement(By.linkText("Click Me!"))).thenReturn(link);
    when(link.getTagName()).thenReturn("a");
    when(link.isEnabled()).thenReturn(true);
    when(link.isDisplayed()).thenReturn(true);

    when(webDriver.findElement(By.linkText("Hover Me!"))).thenReturn(link);
    when(webDriver.getMouse()).thenReturn(mouse);
    when(webDriver.getKeyboard()).thenReturn(keyboard);

    when(link.getCoordinates()).thenReturn(coordinates);

    when(webDriver.findElement(By.name("username"))).thenReturn(input);
    when(input.getTagName()).thenReturn("input");
    when(input.isEnabled()).thenReturn(true);
    when(input.isDisplayed()).thenReturn(true);

    when(webDriver.findElement(By.name("hiddenButton"))).thenReturn(hidden);
    when(hidden.getTagName()).thenReturn("input");
    when(hidden.isEnabled()).thenReturn(true);
    when(hidden.isDisplayed()).thenReturn(false);

    when(webDriver.findElement(By.xpath("//input[@type='checkbox']"))).thenReturn(checkbox);
    when(checkbox.getTagName()).thenReturn("input");
    when(checkbox.isEnabled()).thenReturn(true);
    when(checkbox.isDisplayed()).thenReturn(true);
    when(checkbox.isSelected()).thenReturn(false);

    when(webDriver.executeScript(anyString())).thenReturn(Collections.singletonList("This went wrong!"));

    when(webDriver.findElement(By.className("btn"))).thenReturn(button);
    when(button.getTagName()).thenReturn("button");
    when(button.isEnabled()).thenReturn(false);
    when(button.isDisplayed()).thenReturn(false);
    when(button.getText()).thenReturn("Click Me!");
    when(button.getAttribute("type")).thenReturn("submit");
    when(button.getCssValue("color")).thenReturn("red");

    when(seleniumBrowser.getStoredFile("file.txt")).thenReturn("file.txt");
    Set<String> windows = new HashSet<>();
    windows.add("last_window");
    windows.add("new_window");
    when(webDriver.getWindowHandles()).thenReturn(Collections.singleton("last_window")).thenReturn(windows);
    when(webDriver.getWindowHandle()).thenReturn("last_window");

    context.setApplicationContext(applicationContextMock);
    context.setVariable("cssClass", "btn");

    MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), applicationContextMock, context) {
        @Override//from   w  w w .  ja  va2s  .c o m
        public void execute() {
            selenium(action -> action.start(seleniumBrowser));

            selenium(action -> action.navigate("http://localhost:9090"));

            selenium(action -> action.find().element(By.id("header")));
            selenium(action -> action.find().element("class-name", "${cssClass}").tagName("button")
                    .enabled(false).displayed(false).text("Click Me!").style("color", "red")
                    .attribute("type", "submit"));

            selenium(action -> action.click().element(By.linkText("Click Me!")));
            selenium(action -> action.hover().element(By.linkText("Hover Me!")));

            selenium(action -> action.setInput("Citrus").element(By.name("username")));
            selenium(action -> action.checkInput(false).element(By.xpath("//input[@type='checkbox']")));

            selenium(action -> action.javascript("alert('Hello!')").errors("This went wrong!"));

            selenium(action -> action.alert().text("Hello!").accept());

            selenium(action -> action.clearCache());

            selenium(action -> action.store("classpath:download/file.txt"));
            selenium(action -> action.getStored("file.txt"));

            selenium(action -> action.open().window("my_window"));
            selenium(action -> action.focus().window("my_window"));
            selenium(action -> action.close().window("my_window"));

            selenium(action -> action.waitUntil().hidden().element(By.name("hiddenButton")));

            selenium(action -> action.stop());
        }
    };

    TestCase test = builder.getTestCase();
    int actionIndex = 0;
    Assert.assertEquals(test.getActionCount(), 18);

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StartBrowserAction.class);
    StartBrowserAction startBrowserAction = (StartBrowserAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(startBrowserAction.getName(), "selenium:start");
    Assert.assertNotNull(startBrowserAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            NavigateAction.class);
    NavigateAction navigateAction = (NavigateAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(navigateAction.getName(), "selenium:navigate");
    Assert.assertEquals(navigateAction.getPage(), "http://localhost:9090");
    Assert.assertNotNull(navigateAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            FindElementAction.class);
    FindElementAction findElementAction = (FindElementAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(findElementAction.getName(), "selenium:find");
    Assert.assertEquals(findElementAction.getBy(), By.id("header"));
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            FindElementAction.class);
    findElementAction = (FindElementAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(findElementAction.getName(), "selenium:find");
    Assert.assertEquals(findElementAction.getProperty(), "class-name");
    Assert.assertEquals(findElementAction.getPropertyValue(), "${cssClass}");
    Assert.assertEquals(findElementAction.getTagName(), "button");
    Assert.assertEquals(findElementAction.getText(), "Click Me!");
    Assert.assertFalse(findElementAction.isEnabled());
    Assert.assertFalse(findElementAction.isDisplayed());
    Assert.assertEquals(findElementAction.getStyles().size(), 1L);
    Assert.assertEquals(findElementAction.getStyles().get("color"), "red");
    Assert.assertEquals(findElementAction.getAttributes().size(), 1L);
    Assert.assertEquals(findElementAction.getAttributes().get("type"), "submit");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            ClickAction.class);
    ClickAction clickAction = (ClickAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(clickAction.getName(), "selenium:click");
    Assert.assertEquals(clickAction.getBy(), By.linkText("Click Me!"));
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            HoverAction.class);
    HoverAction hoverAction = (HoverAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(hoverAction.getName(), "selenium:hover");
    Assert.assertEquals(hoverAction.getBy(), By.linkText("Hover Me!"));
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            SetInputAction.class);
    SetInputAction setInputAction = (SetInputAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(setInputAction.getName(), "selenium:set-input");
    Assert.assertEquals(setInputAction.getBy(), By.name("username"));
    Assert.assertEquals(setInputAction.getValue(), "Citrus");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            CheckInputAction.class);
    CheckInputAction checkInputAction = (CheckInputAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(checkInputAction.getName(), "selenium:check-input");
    Assert.assertEquals(checkInputAction.getBy(), By.xpath("//input[@type='checkbox']"));
    Assert.assertFalse(checkInputAction.isChecked());
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            JavaScriptAction.class);
    JavaScriptAction javaScriptAction = (JavaScriptAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(javaScriptAction.getName(), "selenium:javascript");
    Assert.assertEquals(javaScriptAction.getScript(), "alert('Hello!')");
    Assert.assertEquals(javaScriptAction.getExpectedErrors().size(), 1L);
    Assert.assertEquals(javaScriptAction.getExpectedErrors().get(0), "This went wrong!");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            AlertAction.class);
    AlertAction alertAction = (AlertAction) ((DelegatingTestAction) test.getActions().get(actionIndex++))
            .getDelegate();
    Assert.assertEquals(alertAction.getName(), "selenium:alert");
    Assert.assertEquals(alertAction.getText(), "Hello!");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            ClearBrowserCacheAction.class);
    ClearBrowserCacheAction clearBrowserCacheAction = (ClearBrowserCacheAction) ((DelegatingTestAction) test
            .getActions().get(actionIndex++)).getDelegate();
    Assert.assertEquals(clearBrowserCacheAction.getName(), "selenium:clear-cache");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StoreFileAction.class);
    StoreFileAction storeFileAction = (StoreFileAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(storeFileAction.getName(), "selenium:store-file");
    Assert.assertEquals(storeFileAction.getFilePath(), "classpath:download/file.txt");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            GetStoredFileAction.class);
    GetStoredFileAction getStoredFileAction = (GetStoredFileAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(getStoredFileAction.getName(), "selenium:get-stored-file");
    Assert.assertEquals(getStoredFileAction.getFileName(), "file.txt");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            OpenWindowAction.class);
    OpenWindowAction openWindowAction = (OpenWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(openWindowAction.getName(), "selenium:open-window");
    Assert.assertEquals(openWindowAction.getWindowName(), "my_window");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            SwitchWindowAction.class);
    SwitchWindowAction switchWindowAction = (SwitchWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(switchWindowAction.getName(), "selenium:switch-window");
    Assert.assertEquals(switchWindowAction.getWindowName(), "my_window");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            CloseWindowAction.class);
    CloseWindowAction closeWindowAction = (CloseWindowAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(closeWindowAction.getName(), "selenium:close-window");
    Assert.assertEquals(closeWindowAction.getWindowName(), "my_window");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            WaitUntilAction.class);
    WaitUntilAction waitUntilAction = (WaitUntilAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(waitUntilAction.getName(), "selenium:wait");
    Assert.assertEquals(waitUntilAction.getBy(), By.name("hiddenButton"));
    Assert.assertEquals(waitUntilAction.getCondition(), "hidden");
    Assert.assertNotNull(findElementAction.getBrowser());

    Assert.assertEquals(((DelegatingTestAction) test.getActions().get(actionIndex)).getDelegate().getClass(),
            StopBrowserAction.class);
    StopBrowserAction stopBrowserAction = (StopBrowserAction) ((DelegatingTestAction) test.getActions()
            .get(actionIndex++)).getDelegate();
    Assert.assertEquals(stopBrowserAction.getName(), "selenium:stop");
    Assert.assertNotNull(stopBrowserAction.getBrowser());

    Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ALERT_TEXT), "Hello!");
    Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_DOWNLOAD_FILE), "file.txt");
    Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW), "last_window");
    Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "new_window");
    Assert.assertEquals(context.getVariable("my_window"), "new_window");

    verify(alert).accept();
    verify(options).deleteAllCookies();
    verify(link).click();
    verify(input).clear();
    verify(input).sendKeys("Citrus");
}

From source file:com.consol.citrus.selenium.actions.FindElementAction.java

License:Apache License

/**
 * Create by selector from type information.
 * @return/*w w  w.  j  ava2  s.c om*/
 */
protected By createBy(TestContext context) {
    if (by != null) {
        return by;
    }

    switch (property) {
    case "id":
        return By.id(context.replaceDynamicContentInString(propertyValue));
    case "class-name":
        return By.className(context.replaceDynamicContentInString(propertyValue));
    case "link-text":
        return By.linkText(context.replaceDynamicContentInString(propertyValue));
    case "css-selector":
        return By.cssSelector(context.replaceDynamicContentInString(propertyValue));
    case "name":
        return By.name(context.replaceDynamicContentInString(propertyValue));
    case "tag-name":
        return By.tagName(context.replaceDynamicContentInString(propertyValue));
    case "xpath":
        return By.xpath(context.replaceDynamicContentInString(propertyValue));
    }

    throw new CitrusRuntimeException("Unknown selector type: " + property);
}

From source file:com.consol.citrus.selenium.actions.FindElementActionTest.java

License:Apache License

@DataProvider
public Object[][] findByProvider() {
    return new Object[][] { new Object[] { "id", "myId", By.id("myId") },
            new Object[] { "name", "myName", By.name("myName") },
            new Object[] { "tag-name", "button", By.tagName("button") },
            new Object[] { "class-name", "myClass", By.className("myClass") },
            new Object[] { "link-text", "myLinkText", By.linkText("myLinkText") },
            new Object[] { "css-selector", "myCss", By.cssSelector("myCss") },
            new Object[] { "xpath", "myXpath", By.xpath("myXpath") } };
}

From source file:com.consol.citrus.selenium.actions.FindElementActionTest.java

License:Apache License

@Test
public void testExecuteFindByValidation() throws Exception {
    when(element.getText()).thenReturn("Click Me!");
    when(element.getAttribute("type")).thenReturn("submit");
    when(element.getCssValue("color")).thenReturn("red");

    when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() {
        @Override// w  ww.  j a v  a2s.c om
        public WebElement answer(InvocationOnMock invocation) throws Throwable {
            By select = (By) invocation.getArguments()[0];

            Assert.assertEquals(select.getClass(), By.ByName.class);
            Assert.assertEquals(select.toString(), By.name("clickMe").toString());
            return element;
        }
    });

    action.setTagName("button");
    action.setText("Click Me!");
    action.setAttributes(Collections.singletonMap("type", "submit"));
    action.setStyles(Collections.singletonMap("color", "red"));

    action.setProperty("name");
    action.setPropertyValue("clickMe");

    action.execute(context);

    Assert.assertEquals(context.getVariableObject("button"), element);
}

From source file:com.consol.citrus.selenium.actions.FindElementActionTest.java

License:Apache License

@Test(dataProvider = "validationErrorProvider")
public void testExecuteFindByValidationFailed(String tagName, String text, String attribute, String cssStyle,
        boolean displayed, boolean enabled, String errorMsg) throws Exception {
    when(element.getTagName()).thenReturn("button");
    when(element.getText()).thenReturn("Click Me!");
    when(element.getAttribute("type")).thenReturn("submit");
    when(element.getCssValue("color")).thenReturn("red");

    when(webDriver.findElement(any(By.class))).thenAnswer(new Answer<WebElement>() {
        @Override//from w w  w .  j  a va  2  s. c  o  m
        public WebElement answer(InvocationOnMock invocation) throws Throwable {
            By select = (By) invocation.getArguments()[0];

            Assert.assertEquals(select.getClass(), By.ByName.class);
            Assert.assertEquals(select.toString(), By.name("clickMe").toString());
            return element;
        }
    });

    action.setTagName(tagName);
    action.setText(text);
    action.setAttributes(Collections.singletonMap("type", attribute));
    action.setStyles(Collections.singletonMap("color", cssStyle));
    action.setDisplayed(displayed);
    action.setEnabled(enabled);

    action.setProperty("name");
    action.setPropertyValue("clickMe");

    try {
        action.execute(context);
        Assert.fail("Missing exception to to validation error");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().endsWith(errorMsg), e.getMessage());
    }
}

From source file:com.consol.citrus.selenium.client.WebClient.java

License:Apache License

public static By getByFromEnum(ByEnum byEnum, String select) {
    By by = null;//from   ww  w  .j av a  2s  . c  om
    switch (byEnum) {
    case ID:
        by = By.id(select);
        break;
    case CLASS_NAME:
        by = By.className(select);
        break;
    case LINK_TEXT:
        by = By.linkText(select);
        break;
    case CSS_SELECTOR:
        by = By.cssSelector(select);
        break;
    case NAME:
        by = By.name(select);
        break;
    case TAG_NAME:
        by = By.tagName(select);
        break;
    case XPATH:
        by = By.xpath(select);
        break;
    }
    return by;
}