本文整理汇总了Scala中org.openqa.selenium.By类的典型用法代码示例。如果您正苦于以下问题:Scala By类的具体用法?Scala By怎么用?Scala By使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了By类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MessagesPage
//设置package包名称以及导入依赖的类
package uitest.pages
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindBy
import uitest.commands.SeleniumCommands
class MessagesPage(driver: WebDriver) {
private val sc: SeleniumCommands = new SeleniumCommands(driver)
@FindBy(css = "#logoutLink")
val logoutLink: WebElement = null
@FindBy(css = ".alert-info")
val infoAlert: WebElement = null
@FindBy(css = ".alert-danger")
val errorAlert: WebElement = null
@FindBy(css = "textarea")
val messageField: WebElement = null
@FindBy(css = "input[type=submit]")
val sendButton: WebElement = null
def logout() {
logoutLink.click()
sc.waitForFinishLoading()
}
def isUserLogged(user: String): Boolean = {
sc.waitForElementVisible(By.linkText("Logged in as " + user))
true
}
def isUMessageDisplayed(message: String): Boolean = {
val escapedMessage = message.replace("\"", "\\\"")
sc.waitForElementVisible(By.xpath(s"""//span[contains(., "$escapedMessage")]"""))
true
}
def getInfoText: String = {
sc.waitForElementVisible(By.cssSelector(".alert-info"))
infoAlert.getText
}
def getErrorText: String = {
sc.waitForElementVisible(By.cssSelector(".alert-danger"))
errorAlert.getText
}
}
示例2: SeleniumCommands
//设置package包名称以及导入依赖的类
package uitest.commands
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.FluentWait
import org.openqa.selenium.support.ui.Wait
import java.util.concurrent.TimeUnit
class SeleniumCommands(driver: WebDriver) {
final val URL = "http://localhost:8080/#/"
var fluentwait: Wait[WebDriver] = new FluentWait[WebDriver](driver)
.withTimeout(20, TimeUnit.SECONDS)
.pollingEvery(100, TimeUnit.MILLISECONDS)
def waitForFinishLoading() {
waitForElementInvisible(By.cssSelector("#loading-indicator"))
}
def waitForElementClickable(locator: By) {
fluentwait.until(ExpectedConditions.elementToBeClickable(locator))
}
def waitForElementVisible(element: WebElement) {
fluentwait.until(ExpectedConditions.visibilityOf(element))
}
def waitForElementVisible(locator: By) {
fluentwait.until(ExpectedConditions.visibilityOfElementLocated(locator))
}
def waitForElementInvisible(locator: By) {
fluentwait.until(ExpectedConditions.invisibilityOfElementLocated(locator))
}
def waitForElementPresent(locator: By) {
fluentwait.until(ExpectedConditions.presenceOfElementLocated(locator))
}
}
示例3: DOM
//设置package包名称以及导入依赖的类
package jesse.lee.model
import jesse.lee.ops.SeleniumOps
import org.openqa.selenium.{By, WebElement}
case class DOM(element: WebElement) extends SeleniumOps {
def text = element.getText
def text(value: String) = element.sendKeys(value)
def attribute(name: String) = element.getAttribute(name)
def tagName = element.getTagName
def click = element.click()
def clear = element.clear()
def sendKeys(keys: String) = element.sendKeys(keys)
def findItem(by: By): Option[DOM] = safeExecute(DOM(element.findElement(by)))
def findItems(by: By): Seq[DOM] = try {
convertDomElements(element.findElements(by))
} catch {
case e: Throwable => Nil
}
def findItemByLinkText(text: String) = findItem(By.linkText(text))
def findItemsWithCssSelector(css: String): Seq[DOM] = try {
convertDomElements(element.findElements(By.cssSelector(css)))
} catch {
case e: Throwable => Nil
}
def selectOptionWithValue(value: String) = findItemsWithCssSelector("option").reverse.map { option =>
if (option.attribute("value") == value) {
option.click
}
}
}
示例4: PasswordResetPage
//设置package包名称以及导入依赖的类
package uitest.pages
import org.openqa.selenium.{By, WebDriver, WebElement}
import org.openqa.selenium.support.FindBy
import uitest.commands.SeleniumCommands
class PasswordResetPage(driver: WebDriver) {
private val sc: SeleniumCommands = new SeleniumCommands(driver)
def url(code: String) = s"${sc.URL}password-reset?code=$code"
@FindBy(name = "password")
val passwordField: WebElement = null
@FindBy(id = "repeatNotMatching")
val repeatErrorText: WebElement = null
@FindBy(name = "repeatPassword")
val repeatPassField: WebElement = null
@FindBy(css = "button[type=submit]")
val resetButton: WebElement = null
def resetPassword(password: String, repeatedPassword: String) {
passwordField.sendKeys(password)
repeatPassField.sendKeys(repeatedPassword)
resetButton.click()
sc.waitForFinishLoading()
}
def openPasswordResetPage(code: String) {
driver.get(url(code))
sc.waitForFinishLoading()
}
def getErrorText = repeatErrorText.getText
}
示例5: MbankService
//设置package包名称以及导入依赖的类
package services
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.{WebDriverWait, ExpectedCondition}
import org.openqa.selenium.{WebElement, By, WebDriver}
import play.api.Play
class MbankService {
var currentBalance: Double = 0
def getCurrentBalance: Double = {
if (currentBalance == 0) {
implicit val driver: WebDriver = new FirefoxDriver()
driver.get(getConfig("loginPage"))
driver.findElement(By.id("userID")).sendKeys(getConfig("userId"))
driver.findElement(By.id("pass")).sendKeys(getConfig("pass"))
driver.findElement(By.id("submitButton")).click()
val accountBalanceElement = new WebDriverWait(driver, 10).until(
new ExpectedCondition[WebElement] {
override def apply(d: WebDriver) = d.findElement(By.id("currentAccBalance"))
})
currentBalance = accountBalanceElement.getText.replace(',','.').toDouble
}
currentBalance
}
def getConfig(key: String): String = {
val configNamespace: String = "app.services.mbank"
val configKey: String = configNamespace + "." + key
Play.current.configuration.getString(configKey).orNull
}
}
示例6: goOn
//设置package包名称以及导入依赖的类
package uk.gov.hmrc.softdrinksindustrylevyfrontend.util
import java.awt.Robot
import org.openqa.selenium.support.ui.{ExpectedCondition, ExpectedConditions, WebDriverWait}
import org.openqa.selenium.{By, WebDriver, WebElement}
import org.scalatest.concurrent.{Eventually, IntegrationPatience}
import org.scalatest.selenium.WebBrowser
import org.scalatest.{Assertions, Matchers}
import uk.gov.hmrc.softdrinksindustrylevyfrontend.generic.WebPage
trait NavigationSugar extends WebBrowser with Eventually with Assertions with Matchers with IntegrationPatience {
val robot = new Robot()
def goOn(page: WebPage)(implicit webDriver: WebDriver) = {
goTo(page)
on(page)
}
def on(page: WebPage)(implicit webDriver: WebDriver) = {
val wait = new WebDriverWait(webDriver, 5)
wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("body")))
assert(page.isCurrentPage, s"Page was not loaded: ${page.currentUrl}")
}
def notOn(page: WebPage)(implicit webDriver: WebDriver) = {
eventually {
webDriver.findElement(By.tagName("body"))
}
assertResult(false, s"\nDid not expect ${page.currentUrl} to be loaded") {
page.isCurrentPage
}
}
def loadPage()(implicit webDriver: WebDriver) = {
val wait = new WebDriverWait(webDriver, 15)
wait.until(
new ExpectedCondition[WebElement] {
override def apply(d: WebDriver) = d.findElement(By.tagName("body"))
}
)
}
def anotherTabIsOpened()(implicit webDriver: WebDriver) = {
webDriver.getWindowHandles.size() should be(2)
}
def browserGoBack()(implicit webDriver: WebDriver) = {
webDriver.navigate().back()
}
def switchToTabWith(marker: => Boolean)(implicit webDriver: WebDriver): Any = {
windowHandles.foreach { newTab: String =>
switch to window(newTab)
if (marker) return
}
fail(s"Marker evaluation resolves false for current page")
}
}
示例7: PasswordResetPage
//设置package包名称以及导入依赖的类
package uitest.pages
import org.openqa.selenium.{By, WebDriver, WebElement}
import org.openqa.selenium.support.FindBy
import uitest.commands.SeleniumCommands
class PasswordResetPage(driver: WebDriver) {
private val sc: SeleniumCommands = new SeleniumCommands(driver)
def url(code: String) = s"${sc.URL}password-reset?code=$code"
@FindBy(name = "password")
val passwordField: WebElement = null
@FindBy(id = "repeatNotMatching")
val repeatErrorText: WebElement = null
@FindBy(name = "repeatPassword")
val repeatPassField: WebElement = null
@FindBy(css = "button[type=submit]")
val resetButton: WebElement = null
def resetPassword(password: String, repeatedPassword: String) {
passwordField.sendKeys(password)
repeatPassField.sendKeys(repeatedPassword)
resetButton.click()
sc.waitForFinishLoading()
}
def openPasswordResetPage(code: String) {
driver.get(url(code))
sc.waitForFinishLoading()
}
def getErrorText = repeatErrorText.getText
}
示例8: Hello
//设置package包名称以及导入依赖的类
package com.example
import org.openqa.selenium.{By, WebElement}
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import scala.collection.JavaConversions._
object Hello {
def main(args: Array[String]): Unit = {
val url = "http://www.shigemk2.com"
val driver = new HtmlUnitDriver()
driver.get(url)
println(s"title: ${driver.getTitle()}")
driver.findElementByXPath("//div[@id='container']")
.findElements(By.xpath("//a[@class='entry-title-link bookmark']"))
.foreach{ we: WebElement =>
println(s"title: ${we.getText()}")
println(s"link: ${we.getAttribute("href")}")
}
println("Hello, world!")
}
}
示例9: Parser
//设置package包名称以及导入依赖的类
package com.yukimt.scrape
package element
import org.openqa.selenium.{By, WebDriver}
import collection.JavaConversions._
class Parser(driver: WebDriver) {
def getFirst(method:ParserMethod): HtmlElement = method(driver).head
def tryFirst(method:ParserMethod): Option[HtmlElement] = {
try {
method(driver).headOption
} catch {
case e: Throwable =>
println(e.getMessage)
None
}
}
def getAll(method:ParserMethod): Iterable[HtmlElement] = method(driver)
def >>(method:ParserMethod) = getFirst(method)
def >?>(method:ParserMethod) = tryFirst(method)
def >>>(method:ParserMethod) = getAll(method)
}
object ParserMethod{
def by(b: By): ParserMethod = {
val method = (_b: By, driver: WebDriver) =>
driver.findElements(_b).map(e => new HtmlElement(e, driver))
method(b, _)
}
def css(cssQuery: String): ParserMethod = {
val method = (query: String, driver: WebDriver) =>
driver.findElements(By.cssSelector(query)).map(e => new HtmlElement(e, driver))
method(cssQuery, _)
}
}