量角器:如何通过执行js脚本来return元素?
Protractor: How to return element by executing js script?
我的目标是return下拉列表元素。我试图用量角器的方法来做到这一点,但我没有找到搜索 isolate-span 元素的简单方法。
因此我想使用 javasript 代码:
var my_js_element = browser.executeScript(jQuery("td.ng-binding>div.b-combobox.ps-list-drop-single-autocomplete.ng-isolate-scope.ng-pristine.ng-required.ng-invalid.ng-invalid-required").isolateScope().psListDrop.toggleVisible(true).element);
但它不起作用。而且我不确定我是否可以使用此方法 return 元素。是真的吗?或者也许有人知道我该怎么做?
根据 docs of browser.executeScript
:
If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken for resolving this functions return value: - For a HTML element, the value will resolve to a webdriver.WebElement.
从你的 executeScript
调用你应该 return 一个 HTML 元素,它也应该是一个 "native" DOM 元素,所以它可以被转换至 webdriver.WebElement
。然后通过 promises 解析此元素,并在 .then()
:
的回调中作为参数使用
browser.executeScript(function () {
var element = jQuery('.world').get(0); // get "native" DOM node
return element; // explicit return
}).then(function (webElement) {
expect(webElement.getText()).toContain('Hello');
});
所以 Protractor 是建立在 WebDriver
规范之上的。
根据规范,可以从已执行的脚本中获取 return 值。它可能只是 JSON 需要您将其转换回来。您可以阅读更多 here.
尝试记录它 returns 的值。您还可以探索使用 XPATH 选择器来查找您的元素。
类似于:
//td[class="ng-binding"]/div[class="b-combobox" and class="ps-list-drop ...]
我的目标是return下拉列表元素。我试图用量角器的方法来做到这一点,但我没有找到搜索 isolate-span 元素的简单方法。 因此我想使用 javasript 代码:
var my_js_element = browser.executeScript(jQuery("td.ng-binding>div.b-combobox.ps-list-drop-single-autocomplete.ng-isolate-scope.ng-pristine.ng-required.ng-invalid.ng-invalid-required").isolateScope().psListDrop.toggleVisible(true).element);
但它不起作用。而且我不确定我是否可以使用此方法 return 元素。是真的吗?或者也许有人知道我该怎么做?
根据 docs of browser.executeScript
:
If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken for resolving this functions return value: - For a HTML element, the value will resolve to a webdriver.WebElement.
从你的 executeScript
调用你应该 return 一个 HTML 元素,它也应该是一个 "native" DOM 元素,所以它可以被转换至 webdriver.WebElement
。然后通过 promises 解析此元素,并在 .then()
:
browser.executeScript(function () {
var element = jQuery('.world').get(0); // get "native" DOM node
return element; // explicit return
}).then(function (webElement) {
expect(webElement.getText()).toContain('Hello');
});
所以 Protractor 是建立在 WebDriver
规范之上的。
根据规范,可以从已执行的脚本中获取 return 值。它可能只是 JSON 需要您将其转换回来。您可以阅读更多 here.
尝试记录它 returns 的值。您还可以探索使用 XPATH 选择器来查找您的元素。
类似于:
//td[class="ng-binding"]/div[class="b-combobox" and class="ps-list-drop ...]