org.openqa.selenium.By.cssSelector()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中org.openqa.selenium.By.cssSelector()方法的一些代码示例,展示了By.cssSelector()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。By.cssSelector()方法的具体详情如下:
包路径:org.openqa.selenium.By
类名称:By
方法名:cssSelector

By.cssSelector介绍

[英]Find elements via the driver's underlying W3C Selector engine. If the browser does not implement the Selector API, a best effort is made to emulate the API. In this case, we strive for at least CSS2 support, but offer no guarantees.
[中]通过驱动程序的底层W3C选择器引擎查找元素。如果浏览器未实现选择器API,则会尽最大努力模拟该API。在这种情况下,我们至少争取CSS2支持,但不提供任何保证。

代码示例

代码示例来源:origin: selenide/selenide

/**
 * @see By#cssSelector(java.lang.String)
 * @since 3.8
 */
public static By byCssSelector(String css) {
 return By.cssSelector(css);
}

代码示例来源:origin: selenide/selenide

@Override
public SelenideElement find(SelenideElement proxy, Object arg, int index) {
 return arg instanceof By ?
   wrap(driver, proxy, (By) arg, index) :
   wrap(driver, proxy, By.cssSelector((String) arg), index);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testChangeEmailWithoutLogout() throws Exception {
  String newEmail = testChangeEmail(false);
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), containsString("Account Settings"));
  assertThat(webDriver.findElement(By.cssSelector(".alert-success")).getText(), containsString("Email address successfully verified."));
  assertThat(webDriver.findElement(By.cssSelector(".nav")).getText(), containsString(newEmail));
  assertThat(webDriver.findElement(By.cssSelector(".profile")).getText(), containsString(newEmail));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testBuildInfo() throws Exception {
  webDriver.get(baseUrl + "/login");
  String regex = "Version: \\S+, Commit: \\w{7}, Timestamp: .+, UAA: " + baseUrl;
  assertTrue(webDriver.findElement(By.cssSelector(".footer .copyright")).getAttribute("title").matches(regex));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testChangeEmailWithLogout() throws Exception {
  String newEmail = testChangeEmail(true);
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), containsString("Welcome"));
  assertThat(webDriver.findElement(By.cssSelector(".alert-success")).getText(), containsString("Email address successfully verified. Login to access your account."));
  signIn(newEmail, "secr3T");
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), containsString("Where to?"));
}

代码示例来源:origin: cloudfoundry/uaa

private void validateSuccessfulOIDCLogin(String zoneUrl, String userName, String password) {
  login(zoneUrl, userName, password);
  webDriver.findElement(By.cssSelector(".dropdown-trigger")).click();
  webDriver.findElement(By.linkText("Sign Out")).click();
  IntegrationTestUtils.validateAccountChooserCookie(zoneUrl, webDriver);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void codesCanOnlyBeUsedOnce() throws Exception {
  // Go to Forgot Password page
  beginPasswordReset(username);
  String link = getPasswordResetLink(email);
  webDriver.get(link);
  // Attempt to use same code again
  webDriver.get(link);
  assertThat(webDriver.findElement(By.cssSelector(".error-message")).getText(), containsString("Sorry, your reset password link is no longer valid. You can request another one below."));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void resetPassword_displaysErrorMessage_NewPasswordSameAsOld() throws Exception {
  beginPasswordReset(username);
  String link = getPasswordResetLink(email);
  webDriver.get(link);
  webDriver.findElement(By.name("password")).sendKeys("secr3T");
  webDriver.findElement(By.name("password_confirmation")).sendKeys("secr3T");
  webDriver.findElement(By.xpath("//input[@value='Create new password']")).click();
  assertThat(webDriver.findElement(By.cssSelector(".error-message")).getText(), containsString("Your new password cannot be the same as the old password."));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void ldap_login_using_utf8_characters() throws Exception {
  performLdapLogin("testzone2", ldapContainer.nonSslUrl(), true, true, "\u7433\u8D3A", "koala");
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), Matchers.containsString("Where to?"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testHandleForcePasswordChangeInvalidConfirmation() throws Exception {
  navigateToForcePasswordChange();
  webDriver.findElement(By.name("password")).sendKeys("newsecr3T");
  webDriver.findElement(By.name("password_confirmation")).sendKeys("invalid");
  webDriver.findElement(By.xpath("//input[@value='Create new password']")).click();
  assertEquals(baseUrl+"/force_password_change", webDriver.getCurrentUrl());
  assertThat(webDriver.findElement(By.cssSelector(".error-message")).getText(),
    containsString("Passwords must match and not be empty."));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void ldapLogin_with_StartTLS() throws Exception {
  Long beforeTest = System.currentTimeMillis();
  performLdapLogin("testzone2", ldapContainer.nonSslUrl(), true, true, "marissa4", "ldap4");
  Long afterTest = System.currentTimeMillis();
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), Matchers.containsString("Where to?"));
  ScimUser user = IntegrationTestUtils.getUserByZone(zoneAdminToken, baseUrl, "testzone2", "marissa4");
  IntegrationTestUtils.validateUserLastLogon(user, beforeTest, afterTest);
  IntegrationTestUtils.validateAccountChooserCookie(baseUrl.replace("localhost","testzone2.localhost"), webDriver);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testForcePasswordChangeThatFailsPasswordPolicy() {
  navigateToForcePasswordChange();
  String invalidNewPassword = new RandomValueStringGenerator(256).generate();
  webDriver.findElement(By.name("password")).sendKeys(invalidNewPassword);
  webDriver.findElement(By.name("password_confirmation")).sendKeys(invalidNewPassword);
  webDriver.findElement(By.xpath("//input[@value='Create new password']")).click();
  assertEquals(baseUrl+"/force_password_change", webDriver.getCurrentUrl());
  assertThat(webDriver.findElement(By.cssSelector(".error-message")).getText(),
    containsString("Password must be no more than 255 characters in length."));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testUnsuccessfulLogin() throws Exception {
  webDriver.get(baseUrl + "/login");
  assertEquals("Cloud Foundry", webDriver.getTitle());
  attemptLogin(testAccounts.getUserName(), "invalidpassword");
  assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), Matchers.containsString("Welcome!"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void testEnteringContraveningPasswordShowsErrorMessage() {
  startCreateUserFlow(new RandomValueStringGenerator(260).generate());
  assertEquals("Password must be no more than 255 characters in length.", webDriver.findElement(By.cssSelector(".alert-error")).getText());
}

代码示例来源:origin: cloudfoundry/uaa

private String getPasswordResetLink(String email) {
  Iterator receivedEmail = simpleSmtpServer.getReceivedEmail();
  SmtpMessage message = (SmtpMessage) receivedEmail.next();
  receivedEmail.remove();
  assertEquals(email, message.getHeaderValue("To"));
  assertThat(message.getBody(), containsString("Reset your password"));
  Assert.assertEquals("Please check your email for a reset password link.", webDriver.findElement(By.cssSelector(".instructions-sent")).getText());
  // Extract link from email
  return testClient.extractLink(message.getBody());
}

代码示例来源:origin: cloudfoundry/uaa

private void verifyCodeOnRegistration(String key, String expectedUrlPath) {
  GoogleAuthenticator authenticator = new GoogleAuthenticator(new GoogleAuthenticatorConfig.GoogleAuthenticatorConfigBuilder().build());
  Integer verificationCode = authenticator.getTotpPassword(key);
  webDriver.findElement(By.name("code")).sendKeys(verificationCode.toString());
  webDriver.findElement(By.cssSelector("form button")).click();
  assertEquals(zoneUrl + expectedUrlPath, webDriver.getCurrentUrl());
}

代码示例来源:origin: testcontainers/testcontainers-java

@Test
  public void testConnection() {
    RemoteWebDriver driver = chrome.getWebDriver();

    // Construct a URL that the browser container can access
    String hostIpAddress = chrome.getTestHostIpAddress();
    driver.get("http://" + hostIpAddress + ":" + localPort);

    String headingText = driver.findElement(By.cssSelector("h1")).getText().trim();

    assertEquals("The hardcoded success message was found on a page fetched from a local server", "It worked", headingText);
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

protected void doSimpleWebdriverTest(BrowserWebDriverContainer rule) {
  RemoteWebDriver driver = setupDriverFromRule(rule);
  System.out.println("Selenium remote URL is: " + rule.getSeleniumAddress());
  System.out.println("VNC URL is: " + rule.getVncAddress());
  driver.get("http://www.google.com");
  WebElement search = driver.findElement(By.name("q"));
  search.sendKeys("testcontainers");
  search.submit();
  List<WebElement> results = new WebDriverWait(driver, 15)
    .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#search h3")));
  assertTrue("the word 'testcontainers' appears in search results",
    results.stream()
      .anyMatch(el -> el.getText().contains("testcontainers")));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void theHeaderDropdown() throws Exception {
  Assert.assertNotNull(asOnHomePage.getUsernameElement());
  Assert.assertFalse(asOnHomePage.getAccountSettingsElement().isDisplayed());
  Assert.assertFalse(asOnHomePage.getSignOutElement().isDisplayed());
  asOnHomePage.getUsernameElement().click();
  Assert.assertTrue(asOnHomePage.getAccountSettingsElement().isDisplayed());
  Assert.assertTrue(asOnHomePage.getSignOutElement().isDisplayed());
  asOnHomePage.getAccountSettingsElement().click();
  Assert.assertThat(webDriver.findElement(By.cssSelector("h1")).getText(), Matchers.containsString("Account Settings"));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void successfulLoginWithOIDCProviderSetsLastLogin() throws Exception {
  login(zoneUrl, testAccounts.getUserName(), testAccounts.getPassword());
  doLogout(zoneUrl);
  login(zoneUrl, testAccounts.getUserName(), testAccounts.getPassword());
  assertNotNull(webDriver.findElement(By.cssSelector("#last_login_time")));
}

相关文章