Java Code Examples for org.openqa.selenium.By#xpath()

The following examples show how to use org.openqa.selenium.By#xpath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BySelector.java    From selenium with Apache License 2.0 6 votes vote down vote up
public By pickFrom(String method, String selector) {
  if ("class name".equals(method)) {
    return By.className(selector);
  } else if ("css selector".equals(method)) {
    return By.cssSelector(selector);
  } else if ("id".equals(method)) {
    return By.id(selector);
  } else if ("link text".equals(method)) {
    return By.linkText(selector);
  } else if ("partial link text".equals(method)) {
    return By.partialLinkText(selector);
  } else if ("name".equals(method)) {
    return By.name(selector);
  } else if ("tag name".equals(method)) {
    return By.tagName(selector);
  } else if ("xpath".equals(method)) {
    return By.xpath(selector);
  } else {
    throw new WebDriverException("Cannot find matching element locator to: " + method);
  }
}
 
Example 2
Source File: WaitStepsTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void testDoesElementExistsForTimePeriod()
{
    when(webUiContext.getSearchContext()).thenReturn(webElement);
    By xpath =  By.xpath(XPATH);
    IExpectedSearchContextCondition<List<WebElement>> condition = mock(IExpectedSearchContextCondition.class);
    when(expectedSearchContextConditions.presenceOfAllElementsLocatedBy(xpath)).thenReturn(condition);
    IExpectedSearchContextCondition<Boolean> notCondition = mock(IExpectedSearchContextCondition.class);
    when(expectedSearchContextConditions.not(condition)).thenReturn(notCondition);
    WaitResult<Boolean> waitResult = new WaitResult<>();
    waitResult.setWaitPassed(true);
    when(waitActions.wait(webElement, TIMEOUT, notCondition, false)).thenReturn(waitResult);
    waitSteps.doesElementExistsForTimePeriod(XPATH, TIMEOUT.getSeconds());
    verify(softAssert).assertFalse(String.format("Element with xpath '%s' has existed during '%d' seconds",
            XPATH, TIMEOUT.getSeconds()), true);
}
 
Example 3
Source File: LocatorUtilTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetXpathPatternWithNormalization9()
{
    By expectedLocator = By.xpath(".//[text()[normalize-space(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',"
            + " 'abcdefghijklmnopqrstuvwxyz'))='text %'] or *[normalize-space(translate(.,"
            + " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))='text %']]");
    By actualLocator = LocatorUtil.getXPathLocator(true, ELEMENT_WITH_ANY_ATTRIBUTE_OR_TEXT_CASE_INSENSITIVE,
            TEXT_WITH_PERCENT);
    assertEquals(expectedLocator, actualLocator);
}
 
Example 4
Source File: WaitStepsTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testWaitForElementDisappearanceByWithDuration()
{
    By by = By.xpath(XPATH);
    IExpectedSearchContextCondition<Boolean> condition = mock(IExpectedSearchContextCondition.class);
    when(expectedSearchContextConditions.invisibilityOfElement(by)).thenReturn(condition);
    WaitResult<Boolean> waitResult = new WaitResult<>();
    when(waitActions.wait(webElement, TIMEOUT, condition)).thenReturn(waitResult);
    waitResult.setWaitPassed(true);
    assertTrue(waitSteps.waitForElementDisappearance(webElement, by, TIMEOUT));
}
 
Example 5
Source File: CaseInsensitiveTest.java    From carina with Apache License 2.0 5 votes vote down vote up
@Test()
public void testMobileTextLocatorWithDoubleQuotesAndContains() {
	String xpath = "//android.widget.Button[contains(@text, \"Text text\")]";
	By expectedRes = By.xpath("//android.widget.Button[contains(translate(@text, \"TEXT TEXT\", \"text text\"),translate(\"Text text\", \"TEXT TEXT\", \"text text\"))]");
	
	By result = ExtendedElementLocator.toCaseInsensitive(xpath);
    Assert.assertEquals(result, expectedRes, "Incorrect converting to caseinsensitive xpath!");
}
 
Example 6
Source File: CaseInsensitiveTest.java    From carina with Apache License 2.0 5 votes vote down vote up
@Test()
public void testWebTextLocatorWithSingleQuoteAndContains() {
	String xpath = "//div[contains(text(), 'Text text')]";
	By expectedRes = By.xpath("//div[contains(translate(text(), 'TEXT TEXT', 'text text'),translate('Text text', 'TEXT TEXT', 'text text'))]");
	
	By result = ExtendedElementLocator.toCaseInsensitive(xpath);
    Assert.assertEquals(result, expectedRes, "Incorrect converting to caseinsensitive xpath!");
}
 
Example 7
Source File: CaseSensitiveTextSearchTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testFindLinksByTextByLinkText()
{
    By innerTextLocator = By.xpath(String.format(".//*[contains(normalize-space(.), \"%1$s\") and "
            + "not(.//*[contains(normalize-space(.), \"%1$s\")])]", TEXT));
    SearchContext searchContext = mock(SearchContext.class);
    CaseSensitiveTextSearch spy = Mockito.spy(caseSensitiveTextSearch);
    List<WebElement> webElements = List.of(mock(WebElement.class));
    doReturn(List.of()).when(spy).findElementsByText(searchContext, FULL_INNER_TEXT_LOCATOR, parameters, ANY);
    doReturn(webElements).when(spy).findElementsByText(searchContext, innerTextLocator, parameters, ANY);
    List<WebElement> foundElements = spy.search(searchContext, parameters);
    assertEquals(webElements, foundElements);
}
 
Example 8
Source File: LocatorUtilTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetXPathLocatorThroughParentNode3()
{
    By expectedLocator = By.xpath("//*[normalize-space(.//@id)='main-content a-b.C_de2']//h1[text()]");
    By actualLocator = LocatorUtil.getXPathLocator("//*[.//@id='main-content a-b.C_de2']//h1[text()]");
    assertEquals(expectedLocator, actualLocator);
}
 
Example 9
Source File: LocatorUtilTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetXpathPatternWithoutNormalization()
{
    By expectedLocator = By.xpath(".//*[@*='text %' or text()='text %']");
    By actualLocator = LocatorUtil.getXPathLocator(false, ELEMENT_WITH_ANY_ATTRIBUTE_NAME_PATTERN,
            TEXT_WITH_PERCENT);
    assertEquals(expectedLocator, actualLocator);
}
 
Example 10
Source File: LocatorFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@CsvSource({XPATH_PARAM, "xpath(.//a)"})
void convertStringToLocatorTest(String locatorAsString)
{
    By expected = By.xpath(XPATH);
    assertEquals(expected, LocatorFactory.convertStringToLocator(locatorAsString));
}
 
Example 11
Source File: InstructorFeedbackResultsPageUiTest.java    From teammates with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testExceptionalCases() throws Exception {
    ______TS("Case where more than 1 question with same question number");
    // results page should be able to load incorrect data and still display it gracefully

    FeedbackQuestionAttributes firstQuestion = testData.feedbackQuestions.get("qn1InSession4");
    assertEquals(1, firstQuestion.questionNumber);
    FeedbackQuestionAttributes firstQuestionFromDatastore =
                                    BackDoor.getFeedbackQuestion(firstQuestion.courseId,
                                                                 firstQuestion.feedbackSessionName,
                                                                 firstQuestion.questionNumber);

    FeedbackQuestionAttributes secondQuestion = testData.feedbackQuestions.get("qn2InSession4");
    assertEquals(2, secondQuestion.questionNumber);
    // need to retrieve question from datastore to get its questionId
    FeedbackQuestionAttributes secondQuestionFromDatastore =
                                    BackDoor.getFeedbackQuestion(secondQuestion.courseId,
                                                                 secondQuestion.feedbackSessionName,
                                                                 secondQuestion.questionNumber);
    assertEquals(secondQuestion, secondQuestionFromDatastore);
    // make both questions have the same question number
    secondQuestionFromDatastore.questionNumber = 1;
    BackDoor.editFeedbackQuestion(secondQuestionFromDatastore);

    resultsPage = loginToInstructorFeedbackResultsPage("CFResultsUiT.instr", "Session with errors");
    resultsPage.loadResultQuestionPanel(1);
    resultsPage.loadResultQuestionPanel(2);
    // compare html for each question panel
    // to verify that the right responses are showing for each question
    By firstQuestionPanelResponses = By.xpath("//div[contains(@class,'panel')][.//input[@name='questionid'][@value='"
                                         + firstQuestionFromDatastore.getId() + "']]"
                                         + "//div[contains(@class, 'table-responsive')]");
    resultsPage.verifyHtmlPart(firstQuestionPanelResponses,
                               "/instructorFeedbackResultsDuplicateQuestionNumberPanel1.html");

    By secondQuestionPanelResponses = By.xpath("//div[contains(@class,'panel')][.//input[@name='questionid'][@value='"
                                          + secondQuestionFromDatastore.getId() + "']]"
                                          + "//div[contains(@class, 'table-responsive')]");
    resultsPage.verifyHtmlPart(secondQuestionPanelResponses,
                               "/instructorFeedbackResultsDuplicateQuestionNumberPanel2.html");

    ______TS("Results with sanitized data");

    resultsPage = loginToInstructorFeedbackResultsPage("CFResultsUiT.SanitizedTeam.instr",
                                                       "Session with sanitized data");
    resultsPage.loadResultQuestionPanel(1);
    resultsPage.verifyHtmlMainContent("/instructorFeedbackResultsPageWithSanitizedData.html");

    ______TS("Results with sanitized data with comments : giver > recipient > question");

    resultsPage.displayByGiverRecipientQuestion();
    resultsPage.loadResultSectionPanel(0, 1);
    resultsPage.verifyHtmlMainContent("/instructorFeedbackResultsPageGQRWithSanitizedData.html");
}
 
Example 12
Source File: LoginTotpPage.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private By getXPathForLookupCardWithName(String credentialName) {
    return By.xpath("//div[contains(@class, 'card-pf-view-single-select')]//h2[normalize-space() = '"+ credentialName +"']");
}
 
Example 13
Source File: EByXpath.java    From JTAF-ExtWebDriver with Apache License 2.0 4 votes vote down vote up
public EByXpath(String locator) {
    super(locator, By.xpath(locator));
}
 
Example 14
Source File: StringToSeleniumLocatorConverterTests.java    From vividus with Apache License 2.0 4 votes vote down vote up
@Test
void testConverter()
{
    By expected = By.xpath("//xpath");
    assertEquals(expected, converter.convert("By.xpath(//xpath)"));
}
 
Example 15
Source File: LocatorUtil.java    From vividus with Apache License 2.0 4 votes vote down vote up
public static By getXPathLocatorByFullInnerText(String text)
{
    return By.xpath(getXPathByFullInnerTextWithTagName(ANY, text));
}
 
Example 16
Source File: LocatorUtil.java    From vividus with Apache License 2.0 4 votes vote down vote up
public static By getXPathLocatorByInnerTextWithTagName(String tagName, String text)
{
    return By.xpath(getXPathByInnerTextWithTagName(tagName, text));
}
 
Example 17
Source File: AbstractSubmarineIT.java    From submarine with Apache License 2.0 4 votes vote down vote up
protected boolean waitForParagraph(final int paragraphNo, final String state) {
  By locator = By.xpath(getParagraphXPath(paragraphNo)
      + "//div[contains(@class, 'control')]//span[2][contains(.,'" + state + "')]");
  WebElement element = pollingWait(locator, MAX_PARAGRAPH_TIMEOUT_SEC);
  return element.isDisplayed();
}
 
Example 18
Source File: AbstractSubmarineIT.java    From submarine with Apache License 2.0 4 votes vote down vote up
protected void runParagraph(int paragraphNo) {
  By by = By.xpath(getParagraphXPath(paragraphNo) + "//span[@class='icon-control-play']");
  pollingWait(by, 5);
  driver.findElement(by).click();
}
 
Example 19
Source File: Locator.java    From seleniumtestsframework with Apache License 2.0 4 votes vote down vote up
public static By locateByXPath(final String xPath) {
    return By.xpath(xPath);
}
 
Example 20
Source File: ScrollableListObject.java    From functional-tests-core with Apache License 2.0 2 votes vote down vote up
/**
 * Returns locator for all elements.
 *
 * @return
 */
public By getMainContainerItemsLocator() {
    return By.xpath("//" + this.getMainContainerLocatorName() + "//" + this.getMainContainerItemsName());
}