最近开始转 scala 了, 所以就探索了下 scala 的生态. 无意间发现了这样的一个小框架.

http://www.scalatest.org/user_guide/using_selenium

class BlogSpec extends FlatSpec with ShouldMatchers with WebBrowser {

  implicit val webDriver: WebDriver = new HtmlUnitDriver
  val host = "http://localhost:9000/"
  "The blog app home page" should "have the correct title" in {
    go to (host + "index.html")
    pageTitle should be ("Awesome Blog")
    click on id("q")   // to lookup by id "q" 
    click on name("q") // to lookup by name "q" 
    click on linkText("click here!")
    textArea("body").value = "I saw something cool today!"
    textArea("body").value should be ("I saw something cool today!")
  }
}

然后就想是否可以用于 appium 那, 因为都是封装的 webdriver 协议. 于是就实验了下


class AppiumDsl extends FunSuite with ShouldMatchers with WebBrowser with BeforeAndAfterAll{
  val capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName","emulator-5554");
  capabilities.setCapability("platformVersion", "4.4");
  capabilities.setCapability("appPackage", "com.xueqiu.android");
  capabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.xueqiu.android.view.WelcomeActivityAlias")
  capabilities.setCapability("autoLaunch", "true")
  capabilities.setCapability("automationName", "Selendroid")
  capabilities.setCapability(MobileCapabilityType.APP, "/Users/seveniruby/Downloads/xueqiu.apk")
  implicit val driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities)
  //隐式等待设置并不起作用. 这个好像是appium的bug
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
  PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS), this)
  implicitlyWait(Span(10, Seconds))

   test("login xueqiu with dsl"){
    implicitlyWait(Span(10, Seconds))
    click on id("account")
    driver.getKeyboard.sendKeys("156005***")
    click on id("password")
    driver.getKeyboard.sendKeys("*******")
    click on id("button_next")
    click on id("tip_step_one")
    click on id("tip_step_two")
    click on id("tip_step_three")
  }
}

结果发现可以跑在 appium 上. 效果还不错, 就分享下.

不过遗憾的发现好像 selendroid 的 implicitlyWait 并不起作用. 这个好像是 appium 的 bug.
除了这个其他的操作还是挺优雅的.
因为 scala 也是 jvm 体系的. 所以如果把 scala 打包为一个标准的可执行文件. 那么就跟 robotframework 的 appiumlibrary 基本一样简单好用了.


↙↙↙阅读原文可查看相关链接,并与作者交流