【问题标题】:WebDriverIO - Select option from dropdown with dynamic contentWebDriverIO - 从带有动态内容的下拉列表中选择选项
【发布时间】:2020-10-16 13:39:33
【问题描述】:

这是网站,我正在尝试测试: https://opencart.abstracta.us/index.php?route=account/register

有 2 个下拉菜单 - 国家和地区/州,它们相互依赖。 前任。选择美国,在地区/州下拉菜单中,您将看到美国各州等。

我的代码示例:

  const countryDropdown = $(this.demoHomeElement.countryDropdown)
  countryDropdown.selectByAttribute('value', '223');
  //countryDropdown.selectByVisibleText('United States')
  browser.pause(3000);
  countryDropdown.selectByAttribute('value', '223');

  const zoneDropdown = $(this.demoHomeElement.zoneDropdown)
  //zoneDropdown.selectByAttribute('value', '3630');
  zoneDropdown.selectByVisibleText('Florida')
  browser.pause(3000)

问题是当我执行脚本时 - 它选择了正确的选项,但下一个下拉列表中的内容没有更新(显示默认内容,而不是我选择的内容)。

有人可以帮我吗?

例子:

国家/地区下拉菜单 - 选择“美国” 地区/州下拉菜单 - 显示英国的州。

提前致谢!

【问题讨论】:

  • 选择国家后,需要等待网站加载区域。所以你可以在选择国家后尝试暂停(3000)。
  • browser.pause 有帮助吗?
  • 不,浏览器暂停不起作用。
  • 我认为它应该通过“执行”命令来工作,应该编写一个脚本来将选中的选项更改为选项。

标签: javascript selenium-webdriver webdriver-io


【解决方案1】:

看看我是怎么做到的,它在 Chrome 中运行良好,没有在其他浏览器中检查:

describe("Test scenario", () => {
  it("Select drop downs", () => {
    browser.url(
      "https://opencart.abstracta.us/index.php?route=account/register"
    );

    $("//h1[text()='Register Account']").waitForDisplayed(); // just check that page elements are loaded on the page

    const selectBoxOne = $("#input-country");
    const selectBoxTwo = $("#input-zone");
    const spinner = $(
      "//i[contains(@class,'fa') and contains(@class,'fa-circle-o-notch') and contains(@class,'fa-spin')]"
    ); // after selecting BoxOne there will be a spinner...

    selectBoxOne.selectByAttribute("value", "103"); // Ireland

    browser.waitUntil(() => spinner.isDisplayed() == false); // wait for spinner to go away

    selectBoxTwo.selectByVisibleText("Dublin");
    browser.pause(5000); // Just to get a chance to see it
  });
});

注意:无论您在第一个下拉菜单中选择什么,DOM 元素中的选定值都将保持不变为United Kingdom

我希望这会有所帮助。

还为开始使用 WebdriverIO here的人们写了一篇文章。

【讨论】:

  • 嗯,是的,它对我的​​工作方式相同。有什么办法可以改变选中的选项吗?我的意思是,有什么方法可以强制使用美国并一直显示美国的州,而不是英国?
  • 太好了,如果它有效,请您接受答案。关于所选选项,我发现如果您手动执行此操作,则会出现相同的行为,因此我怀疑您需要更改应用程序代码。
  • 如果你想在测试执行期间强制一个特定的
  • 如果您需要更大的灵活性,请检查browser.execute 语法:webdriver.io/docs/api/browser/execute.html
  • 请不要犹豫,接受这个答案,因为它对你有用,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2012-01-11
相关资源
最近更新 更多