简体   繁体   English

如何在 webdriverIO 中单击 input type=radio

[英]How to click input type=radio in webdriverIO

I'm trying to write some tests using WDIO but stumble on the following issue.我正在尝试使用 WDIO 编写一些测试,但偶然发现了以下问题。 I don't really understand how to click() input type=radio.我真的不明白如何 click() input type=radio。 Here how it looks in DOM这是它在 DOM 中的样子在此处输入图片说明

So I need to click the right one.所以我需要点击正确的。 I found it with $([id="all"]) and after waiting untill it's displayed use click() method and nothing happens.我用 $([id="all"]) 找到了它,等到它显示出来之后,使用 click() 方法并没有任何反应。 I'll apreciate any help我会感谢任何帮助

Without seeing the actual code you're using, the best we can do is assume that maybe the selector you're using needs to be adjusted.在没有看到您使用的实际代码的情况下,我们能做的最好的事情就是假设您使用的选择器可能需要调整。 For instance, the $ method in WebdriverIO takes a selector as a string, so $('[id="all"]').click() should work, since the selector is inside single-quotes with the attribute value in double-quotes.例如,WebdriverIO 中的$方法将选择器作为字符串,因此$('[id="all"]').click()应该可以工作,因为选择器位于单引号内,属性值为双引号引号。

Also, it may be easier to simply use the hash symbol for id attributes:此外,简单地将哈希符号用于 id 属性可能更容易:

$('#all').click();

A good way to debug things like this is to use the REPL.调试此类事情的一个好方法是使用 REPL。 With the REPL, you can manually browse to the page with your radio buttons and then run arbitrary WebdriverIO commands from the terminal in order to test them out.使用 REPL,您可以使用单选按钮手动浏览到页面,然后从终端运行任意 WebdriverIO 命令以测试它们。 Assuming they work, then the problem must be something with your waits.假设它们有效,那么问题一定与您的等待有关。

Also, check to make sure the element with id="all" is unique.另外,检查以确保 id="all" 的元素是唯一的。 If there are multiple elements on the page with id="all" then that would be violating the HTML spec, since id attribute values must be unique on a single page.如果页面上有多个 id="all" 元素,那么这将违反 HTML 规范,因为 id 属性值在单个页面上必须是唯一的。

To use the REPL, run the following command:要使用 REPL,请运行以下命令:

$ npx wdio repl chrome

This will open a browser, which you can control manually, as well as by running WebdriverIO commands from the terminal.这将打开一个浏览器,您可以手动控制该浏览器,也可以从终端运行 WebdriverIO 命令。

From what you've mentioned in the comments, you need to select an element that's inside a ShadowDOM.根据您在评论中提到的内容,您需要选择 ShadowDOM 中的一个元素。 WebdriverIO has an API for this, which looks something like this: WebdriverIO 有一个 API,它看起来像这样:

$(selector).shadow$(selector);

The first selector will point to the element that contains the shadowRoot, and the second selector is for the input radio button element.第一个选择器将指向包含 shadowRoot 的元素,第二个选择器用于输入单选按钮元素。 For instance, let's say this is your HTML:例如,假设这是您的 HTML:

<some-custom-element>
  #shadow-root
    <header>
      <h1>
      <input type="radio" id="all">

To access, and click on, the radio button, we'd use the following WebdriverIO code:要访问并单击单选按钮,我们将使用以下 WebdriverIO 代码:

$('some-custom-element').shadow$('input#all');

Your waitFors would work the same.您的 waitFors 将工作相同。 If we're waiting for something in the shadow DOM to appear, such as the header, we'd do this:如果我们正在等待 shadow DOM 中的某些内容出现,例如标题,我们会这样做:

$('some-custom-element').shadow$('header').waitForDisplayed();

Although the above examples use element selectors, we can also select the elements with id's, classes, or other attributes as well.虽然上面的例子使用了元素选择器,我们也可以选择带有 id's、classes 或其他属性的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM