Java Code Examples for org.openqa.selenium.WebElement.click()

The following are Jave code examples for showing how to use click() of the org.openqa.selenium.WebElement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: marathonv5   File: NativeEventsTest.java   View Source Code Vote up 7 votes
public void draggedGeneratesSameEvents() throws Throwable {
    events = MouseEvent.MOUSE_DRAGGED;
    SwingUtilities.invokeAndWait(new Runnable() {
        @Override public void run() {
            actionsArea.setText("");
        }
    });
    driver = new JavaDriver();
    WebElement b = driver.findElement(By.name("click-me"));
    WebElement t = driver.findElement(By.name("actions"));

    Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
    Dimension size = EventQueueWait.call_noexc(button, "getSize");
    Robot r = new Robot();
    r.setAutoDelay(10);
    r.setAutoWaitForIdle(true);
    Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
    Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");

    r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
    r.mouseRelease(InputEvent.BUTTON1_MASK);
    new EventQueueWait() {
        @Override public boolean till() {
            return actionsArea.getText().length() > 0;
        }
    }.wait("Waiting for actionsArea failed?");
    String expected = t.getText();
    tclear();
    r.mousePress(InputEvent.BUTTON1_MASK);
    r.mouseRelease(InputEvent.BUTTON1_MASK);

    b.click();
    tclear();
    System.err.println("============================");
    new Actions(driver).clickAndHold(b).moveToElement(b).release().perform();
    System.err.println("============================");
    AssertJUnit.assertEquals(expected, t.getText());
}
 
Example 2
Project: hippo   File: InformationTypeCmsWidget.java   View Source Code Vote up 6 votes
public void addInformationTypeField() {
    // get element
    WebElement addButton = helper.findElement(By.xpath(ROOT_ELEMENT_XPATH + "//a[contains(@class, 'add-link')]"));

    // scroll to element, to prevent errors like "Other element would receive the click"
    new Actions(webDriver)
        .moveToElement(addButton)
        .moveByOffset(0, 200)
        .perform();

    // click
    addButton.click();

    // wait after click for the dropdown to appear
    helper.findElement(By.xpath(DROPDOWN_XPATH));
}
 
Example 3
Project: Cognizant-Intelligent-Test-Scripter   File: CheckBox.java   View Source Code Vote up 6 votes
@Action(object = ObjectType.BROWSER, desc = "Check all the check boxes in the context")
public void checkAllCheckBoxes() {
    try {
        List<WebElement> checkboxes = Driver.findElements(By.cssSelector("input[type=checkbox]"));
        if (checkboxes.isEmpty()) {
            Report.updateTestLog(Action, "No Checkbox present in the page", Status.WARNING);
        } else {
            for (WebElement checkbox : checkboxes) {
                if (checkbox.isDisplayed() && !checkbox.isSelected()) {
                    checkbox.click();
                }
            }
            Report.updateTestLog(Action, "All checkboxes are checked", Status.PASS);
        }
    } catch (Exception ex) {
        Report.updateTestLog(Action, "Error while checking checkboxes - " + ex, Status.FAIL);
        Logger.getLogger(CheckBox.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 4
Project: marathonv5   File: JTreeDynamicTreeTest.java   View Source Code Vote up 6 votes
public void expandTree() throws Throwable {
    WebElement tree = page.getTree();
    tree.click();
    WebElement root = tree.findElement(By.cssSelector(".::nth-node(1)"));
    AssertJUnit.assertEquals("false", root.getAttribute("expanded"));
    AssertJUnit.assertEquals(1 + "", tree.getAttribute("rowCount"));
    new Actions(driver).doubleClick(root).perform();
    new WebDriverWait(driver, 3).until(hasAttributeValue(root, "expanded", "true"));
    AssertJUnit.assertEquals("true", root.getAttribute("expanded"));
    AssertJUnit.assertEquals(3 + "", tree.getAttribute("rowCount"));
    WebElement node1 = tree.findElement(By.cssSelector(".::nth-node(2)"));
    AssertJUnit.assertEquals("Parent 1", node1.getText());
    new Actions(driver).doubleClick(node1).perform();
    WebElement node2 = tree.findElement(By.cssSelector(".::nth-node(3)"));
    AssertJUnit.assertEquals("Child 1", node2.getText());
    WebElement node3 = tree.findElement(By.cssSelector(".::nth-node(4)"));
    AssertJUnit.assertEquals("Child 2", node3.getText());
    WebElement node4 = tree.findElement(By.cssSelector(".::nth-node(5)"));
    AssertJUnit.assertEquals("Parent 2", node4.getText());
    new Actions(driver).doubleClick(node4).perform();
    WebElement node5 = tree.findElement(By.cssSelector(".::nth-node(6)"));
    AssertJUnit.assertEquals("Child 1", node5.getText());
    WebElement node6 = tree.findElement(By.cssSelector(".::nth-node(7)"));
    AssertJUnit.assertEquals("Child 2", node6.getText());
}
 
Example 5
Project: ServiceNow_Selenium   File: ServiceNow.java   View Source Code Vote up 5 votes
/**
 * @category Set Element
 */
private void typeInElement(WebElement element, String content) {
	_action.moveToElement(element);
	switch (this.getFieldType(element)) {
	case "choice":
		List<WebElement> options = element.findElements(By.tagName("option"));
		for (int i = 0; i < options.size(); i++) {
			if (options.get(i).getText().trim().equalsIgnoreCase(content)) {
				element.click();
				options.get(i).click();
				break;
			}
		}
		break;
	default:
		_action.click();
		if(!this.getFieldValueByID(element.getAttribute("id")).equalsIgnoreCase("")) {
			//TODO - Find a faster way to do this.
			_action.sendKeys(Keys.END);
			for (int i = 0; i <this.getFieldValueByID(element.getAttribute("id")).length(); i ++) {
				_action.sendKeys(Keys.BACK_SPACE);
			}

		}
		_action.sendKeys(content);
		_action.sendKeys(Keys.TAB);
	_action.perform();
	}

}
 
Example 6
Project: vu-timetable   File: Navigator.java   View Source Code Vote up 5 votes
private void selectSemester() {
    final WebElement selectElement = driver.findElement(By.id("lbWeeks"));
    for (WebElement option : selectElement.findElements(By.tagName("option"))) {
        if (option.getText().toLowerCase().contains("semester")) {
            option.click();
            return;
        }
    }
}
 
Example 7
Project: selenium-components   File: SelectComponent.java   View Source Code Vote up 5 votes
public void selectByLabel(String label)
{
    WebElement element = element();

    element.click();
    element.sendKeys(label);
    element.sendKeys(Keys.RETURN);
}
 
Example 8
Project: seleneasy   File: Seleneasy.java   View Source Code Vote up 5 votes
/**
 * Positionate in an element naturally (scrolling and clicking)
 * @param webElement Element
 * @return Element
 * @throws Exception
 */
public WebElement positionateAndClick(WebElement element) throws Exception {
    Actions mouseActions = new Actions(driver);
    try {
        mouseActions.moveToElement(element).build().perform();
        mouseActions.click(element).build().perform();
    } catch (Exception e) {
        //not moveable
        element.click();
    }
    
    return element;
}
 
Example 9
Project: marathonv5   File: JTreeDynamicTreeTest.java   View Source Code Vote up 5 votes
public void nodeEditor() throws Throwable {
    System.err.println("Ignore the following NPE. The DynamicTree class has a bug");
    WebElement tree = page.getTree();
    tree.click();
    WebElement root = tree.findElement(By.cssSelector(".::nth-node(1)"));
    AssertJUnit.assertEquals("Root Node", root.getText());
    WebElement editor = root.findElement(By.cssSelector(".::editor"));
    editor.clear();
    editor.sendKeys("Hello World", Keys.ENTER);
    root.submit();
    AssertJUnit.assertEquals("Hello World", root.getText());
}
 
Example 10
Project: WebAndAppUITesting   File: BaseOpt.java   View Source Code Vote up 5 votes
/**
 * 点击元素
 * 
 * @param element
 */
public void click(WebElement element) {
	try {
		element.click();
		LogUtil.debug(getDriver().manage().logs() + "==>" + element.toString() + " 点击成功!");
	} catch (Exception e) {
		LogUtil.error(getDriver().manage().logs() + "==>" + element.toString() + " 点击失败!" + e);
		screenShot();
		AssertUtil.fail(getDriver().manage().logs() + "==>" + element.toString() + " 点击失败!" + e);
	}
}
 
Example 11
Project: selenium-jupiter   File: AppiumApkJupiterTest.java   View Source Code Vote up 5 votes
@Test
void testWithAndroid(AppiumDriver<MobileElement> driver)
        throws InterruptedException {
    WebElement button = driver.findElement(By.id("buttonStartWebview"));
    button.click();

    WebElement inputField = driver.findElement(By.id("name_input"));
    inputField.clear();
    inputField.sendKeys("Custom name");
}
 
Example 12
Project: testing_security_development_enterprise_systems   File: HomePageObject.java   View Source Code Vote up 5 votes
public void setShowOnlyOwnCountry(boolean value) {

        List<WebElement> elements = driver.findElements(By.id("showOnlyOwnCountryForm:showOnlyOwnCountry"));
        WebElement e = elements.get(0);

        if ((value && !e.isSelected()) || (!value && e.isSelected())) {
            e.click();
            waitForPageToLoad();
        }
    }
 
Example 13
Project: appium_tutorial   File: AndroidTest.java   View Source Code Vote up 5 votes
@Test
public void MenuOverview(){
	WebDriverWait wait = new WebDriverWait(driver, 10);
	WebElement menuButton = driver.findElement(By.xpath("//android.widget.ImageButton[@content-desc=\"Open navigation\"]"));

	menuButton.click();
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Overview\"]"))));

    WebElement overViewButton = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Overview\"]"))));
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Accounts\"]"))));
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Transactions\"]"))));
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Reports\"]"))));
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@text=\"Settings\"]"))));
    overViewButton.click();
}
 
Example 14
Project: devtools-driver   File: ExampleMobileSafariWebTest.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
  // Create a DesiredCapabilities object to request specific devices from the WebDriver server.
  // A udid can be optionally specified, otherwise an arbitrary device is chosen.
  DesiredCapabilities caps = new DesiredCapabilities();
  // caps.setCapability("uuid", udid);
  // Start a WebDriver session. The local machine has to be running the SafariDriverServer, or
  // change localhost below to an IP running the SafariDriverServer.
  driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), caps);
  // Connect to a URL
  driver.get("http://www.google.com");

  // Interact with the web page. In this example use case, the Webdriver API is used to find
  // specific elements, test a google search and take a screenshot.
  driver.findElement(By.id("hplogo"));

  // Google New York
  WebElement mobileSearchBox = driver.findElement(By.id("lst-ib"));
  mobileSearchBox.sendKeys("New York");
  WebElement searchBox;
  try {
    searchBox = driver.findElement(By.id("tsbb"));
  } catch (NoSuchElementException e) {
    searchBox = driver.findElement(By.name("btnG"));
  }
  searchBox.click();

  takeScreenshot();
  driver.navigate().refresh();
  takeScreenshot();

  // Quit the WebDriver instance on completion of the test.
  driver.quit();
  driver = null;
}
 
Example 15
Project: marathonv5   File: JListXTest.java   View Source Code Vote up 5 votes
public void listClickingOnAnItemSelectsIt() throws Throwable {
    EventQueueWait.waitTillShown(list);
    driver = new JavaDriver();
    WebElement listItem;
    listItem = driver.findElement(By.cssSelector("#list-1::nth-item(21)"));
    assertEquals("List Item - 21", listItem.getText());
    listItem.click();
    assertEquals("20", driver.findElement(By.cssSelector("#list-1")).getAttribute("selectedIndex"));
    listItem = driver.findElement(By.cssSelector("#list-1::nth-item(3)"));
    assertEquals("List Item - 3", listItem.getText());
    listItem.click();
    assertEquals("2", driver.findElement(By.cssSelector("#list-1")).getAttribute("selectedIndex"));
}
 
Example 16
Project: BrainBridge   File: BrainInstance.java   View Source Code Vote up 5 votes
/**
 * Shuts this instance down and frees all used resources.
 */
public void shutdown() {
	switchToWindow();
	switchToFrame(CHAT_INPUT_FRAME_NAME);

	final WebElement logoutAnchor = new CSSSelectorPresenceWait(this.mDriver, LOGOUT_ANCHOR).waitUntilCondition();
	logoutAnchor.click();

	this.mDriver.close();
}
 
Example 17
Project: selenium-testng-template   File: SeleniumUtils.java   View Source Code Vote up 4 votes
public static final void click(By by){
	SeleniumWaitUtils.waitForElement(by);
	WebElement clickElement = Config.driver().findElement(by);
	clickElement.click();
}
 
Example 18
Project: testing_security_development_enterprise_systems   File: GitCrawler.java   View Source Code Vote up 4 votes
private static void scanCurrentPage(WebDriver driver, int page) {
    String current = driver.getCurrentUrl();

    //Note: there was a layout change in the past on Github page
    //String xpath = "//h3[@class='repo-list-name']/a";
    String xpath = "//div[@class='container']//ul//h3//a";

    List<WebElement> projects = driver.findElements(By.xpath(xpath));
    List<String> names = projects.stream().map(p -> p.getText()).collect(Collectors.toList());

    System.out.println("Going to analyze "+names.size()+" projects in this page at: "+current);

    while (!names.isEmpty()) {

        String name = names.remove(0);

        try {
            /*
                do not overflow GitHub of requests. 10 per minutes should be more than enough.
                Recall: "If you would like to crawl GitHub contact us at [email protected]"
                from  https://github.com/robots.txt
             */
            Thread.sleep(6000);
        } catch (InterruptedException e) {
        }

        By byName = By.xpath(xpath + "[text()='" + name + "']");
        WebElement a = getElement(driver, byName, page);
        a.click();
        Boolean loaded = waitForPageToLoad(driver);

        if (loaded) {

            /*
                Checking if either Maven or Gradle

                Note: not a robust check:
                - build files might not be in root folder
                - no check if there was any error in the loaded page
             */
            List<WebElement> maven = driver.findElements(By.xpath("//td[@class='content']//a[@title='pom.xml']"));
            if (!maven.isEmpty()) {
                System.out.println("" + name + " uses Maven");
            } else {
                List<WebElement> gradle =  driver.findElements(By.xpath("//td[@class='content']//a[@title='build.gradle']"));
                if (!gradle.isEmpty()) {
                    System.out.println("" + name + " uses Gradle");
                } else {
                    System.out.println("" + name + " undefined build system");
                }
            }
        }

        driver.get(current);
        waitForPageToLoad(driver);
    }
}
 
Example 19
Project: testing_security_development_enterprise_systems   File: PageObject.java   View Source Code Vote up 4 votes
public void clickAndWait(String id){
    WebElement element = driver.findElement(By.id(id));
    element.click();
    waitForPageToLoad();
}
 
Example 20
Project: moviediary   File: UiCommonTest.java   View Source Code Vote up 4 votes
private void checkCanGetToPage(WebElement link, String urlToCheck) {
    sleep(driver, 5, invisibilityOf(driver.findElementById("loader-wrapper")));
    sleep(driver, 5, elementToBeClickable(link));
    link.click();
    assertEquals(urlToCheck, driver.getCurrentUrl());
}