如何将webdriverio驱动程序传递给模块化设计的功能?

时间:2018-07-04 09:19:26

标签: javascript appium webdriver-io

我正在使用webdriverio与appium服务器通信。我正在尝试自动化Instagram交互。每个自动化都需要我登录和注销。因此,我正在考虑编写一个登录功能和一个注销功能。

目前,我有这样的东西:

const wdio = require("webdriverio");
const opts = {...}
const client = wdio.remote(opts)

const login = (client, username, password) => {
  return client
    .click("#log_in_button")
    .click("#login_username")
    .keys(username)
    .click("#password")
    .keys(password)
    .click("#button_text");
}

const someOtherAction = (client) => {
  // More actions performed on client
  // ...
}

client.init();
login(client, "username", "password")
  .someOtherAction(client);

这不起作用^^,但是以这种方式编写的相同代码有效:

client
  .init()
  .click("#log_in_button")
  .click("#login_username")
  .keys("username")
  .click("#password")
  .keys("password")
  .click("#button_text")
  .waitForExist("~Activity") // Some other actions...

我怀疑这与this设置不正确有关,我尝试查看webdriverio的来源,但我不太了解。那么,如何传递驱动程序,以便编写更多模块化,可用和可维护的代码?

此外,我还不太了解这种命令链接是如何工作的,因为http://webdriver.io的api文档未声明有关click,{{ 1}}等,但是,它们似乎像在返回keys本身一样工作。如果有人也可以解释链接,那就太好了。

2 个答案:

答案 0 :(得分:3)

使用WebdriverIO编写测试的推荐方法是通过PageObject模式。 GitHub存储库中有一个example目录。检查boilerplate section也是一个好主意,WebdriverIO社区的人们在那里提供了有用的入门包,以使他们快速入门。

答案 1 :(得分:0)

因此,我通过使登录成为/** * Created by Hidro Le. * Job Title: Magento Developer * Date: 27/08/2018 * Time: 10:16 */ define(['jquery', 'slickSlider'], function ($) { "use strict"; return function (config, element) { let defaultConfig = { infinite: true, slidesToShow: 3, slidesToScroll: 3 }; $(element).slick($.extend({}, defaultConfig, config)); }; }); 函数并async进行操作来实现了模块化。

await

然后将其用作:

const login = async (client, username, password) => {
  await client
   .click("#log_in_button")
   .click("#login_username")
   .keys(username)
   .click("#password")
   .keys(password)
   .click("#button_text");
}