【问题标题】:How to select an element in ng-autocomplete in protractor?如何在量角器的 ng-autocomplete 中选择一个元素?
【发布时间】:2016-12-05 12:55:41
【问题描述】:

通过文本传递

sendKeys("dublin,south Africa");

无法选择自动完成中的第一个元素。

【问题讨论】:

  • sendKeys 只会在文本字段中输入文本。您需要使您的脚本从自动完成下拉菜单中单击所需的值。您能否提供有关您当前面临的问题的更多信息?
  • 我有一个位置字段,当用户输入城市名称时,它会识别并给出建议的城市名称。@SudharsanSelvaraj
  • 所以你需要做的是在输入城市名称后等待下拉建议显示。然后从建议列表中点击所需的城市名称。
  • yes.it 应该从@SudharsanSelvaraj 列表中选择第一个

标签: angularjs autocomplete protractor gulp-protractor protractor-net


【解决方案1】:

问题已解决。

this.checkin = function(text,index){
        element( by.css('[ng-click="showMapTextBox2()"]') ).click();
        // element(by.model("location")).sendKeys(text);
        browser.actions()
        .mouseMove(element(by.model("location"))
        .sendKeys(text))
        .perform().then(function(){
        browser.sleep(500);
        // press the down arrow for the autocomplete item we want to choose
        for(i = 0; i < index ; i++){

            browser.actions().sendKeys(protractor.Key.ARROW_DOWN).perform();
        }
        browser.sleep(500);
        browser.actions().sendKeys(protractor.Key.ENTER).perform();
      });
        browser.sleep(3000);
    };

spec_test 代码:

post_text.checkin("new Delhi, India",1);

【讨论】:

    【解决方案2】:

    手动键入您希望测试执行的内容并检查自动完成的元素。您将使用 protractor.ExpectedConditions 等待该元素,然后在发送密钥后单击它。

    【讨论】:

    • 仍然面临同样的问题@doct03
    【解决方案3】:

    你需要做两件事:

    1. 输入文本。有时需要按 Tab 来强制自动完成显示选项
    2. 选择适当的下拉选项

    C# 中的示例代码:

    fieldElement.ClearAndSendKeys(partialValue);
    fieldElement.SendKeys(Keys.Tab);
    GetFieldDropdown(completeValue).Click();
    

    GetFielDropdown() 方法的详细信息取决于您的 DOM。这是我正在从事的一个项目的简化示例:

    private IWebElement GetFieldDropdown(string dropdownValue)
    {
        return FindElement(By.XPath($"//span[contains(.,'{dropdownValue}')]"));
    } 
    

    请注意,如果自动完成延迟显示在上面的代码中,代码不会改变(FindElement 不会返回元素)。您需要等待下拉选项显示后才能单击它。

    PS - partialValue 和 completeValue 可以相同

    【讨论】:

    • 感谢@Ory Zaidenvorm。写成这样的代码。 this.checkin = function(value){ var EC=protractor.ExpectedConditions;元素( by.css('[ng-click="showMapTextBox2()"]') ).click(); var enterLocation = element(by.model("location")); browser.wait(EC.visibilityOf(enterLocation),10000,'enterLocation not found'); browser.sleep(5000); var ExpList = enterLocation.sendKeys(value);期望(ExpList.isDisplayed()).toBe(true); expect(ExpList.getText()).toEqual('海得拉巴,特伦甘纳邦,印度'); browser.sleep(4000); };
    • @Shiva 我认为您在 var ExpList = enterLocation.sendKeys(value); 行中有问题您应该 SendKeys 然后设置(查找)ExpList(与我在提供的示例中的 GetFileDropdown 方法中所做的相同)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多