gpt4 book ai didi

javascript - Protractor 测试 chrome 不会点击项目,而 safari 会

转载 作者:行者123 更新时间:2023-11-30 16:43:16 25 4
gpt4 key购买 nike

我的测试在 safari 中顺利通过,而在 chrome 中这个特定位似乎不起作用:

 it('should click the first source and get to the source preview page', function () {
var grid_icon = element(by.css('.fa-th'));
var sources = element.all(by.repeater('source in shownSources'));

sources.get(0).element(by.tagName('a')).click();
browser.pause();
// Check url
expect(browser.getCurrentUrl()).toContain('/source/');
});

点击超链接后,它应该变为包含“/source/”的 url。这在 Safari 中工作得很好,但在 Chrome 中它失败了

我的 Protractor 配置文件:

exports.config = {
framework: 'jasmine2',
seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
seleniumPort: 4444,
troubleshoot: false,
basePath: '../',
specs: ['protractor/***/**/*.js'],
baseUrl: 'http://localhost:9000',
capabilities: {
browserName: 'chrome'
},
onPrepare: function() {
browser.manage().window().maximize();
}
};

编辑:我最初的问题似乎不再发生了。但是测试仍然表现得很奇怪。这一点在 Safari 中工作得很好:

it('should add all sources to the list and show the cart icon on the source in inventory', function () {
browser.get('/sources');
var ordersources = element.all(by.repeater('orderSource in orderSources'));
var sources = element.all(by.repeater('source in shownSources'));

sources.get(0).element(by.css('a')).click();
var add_to_cart_btn = element(by.binding('addBtnText'));
add_to_cart_btn.click();
browser.get('/sources');

sources.get(1).element(by.css('a')).click();
var add_to_cart_btn = element(by.binding('addBtnText'));
add_to_cart_btn.click();
browser.get('/sources');

browser.pause();

sources.get(2).element(by.css('a')).click();
var add_to_cart_btn = element(by.binding('addBtnText'));
add_to_cart_btn.click();
browser.get('/sources');

expect(ordersources.count()).toBe(3);

sources.each(function (field) {
var isInCart_symbol = field.element(by.css('.fa-cart-arrow-down'));
expect(isInCart_symbol.getAttribute('aria-hidden')).toBe('false');
});
});

但是在 Chrome 中,第二次找不到“a”项,并且 browser.sleep() 永远不会执行,下一个“it”开始运行。

编辑:我通过使用类属性的另一个 html 元素让它工作。

element.(by.css('.example'))

最佳答案

我假设当你说它失败时,期望失败了?您可以尝试以下 3 种方法。

// Wait Till Url Contains
function WaitTillUrlContains(text, time, errMessage){
if(typeof(time) ==='undefined') time = 5000;
browser.getCurrentUrl().then(function (currentUrl) {
browser.wait(function () {
return browser.getCurrentUrl().then(function (newUrl) {
var test = newUrl;
if( test.indexOf(text) >= 0){
// Found word
return true;
}

});
}, time , errMessage);
});
};

(1) 在expect之前加一个wait。

it('should click the first source and get to the source preview page', function () {
var grid_icon = element(by.css('.fa-th'));
var sources = element.all(by.repeater('source in shownSources'));

sources.get(0).element(by.tagName('a')).click();

// Check url
WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
expect(browser.getCurrentUrl()).toContain('/source/');
});

(2) 点击后执行.then()函数

it('should click the first source and get to the source preview page', function () {
var grid_icon = element(by.css('.fa-th'));
var sources = element.all(by.repeater('source in shownSources'));

sources.get(0).element(by.tagName('a')).click().then(function(){

// Check url
WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
expect(browser.getCurrentUrl()).toContain('/source/');
});
});

(3) 获取元素后执行.then()函数然后点击

it('should click the first source and get to the source preview page', function () {
var grid_icon = element(by.css('.fa-th'));
var sources = element.all(by.repeater('source in shownSources'));

sources.get(0).element(by.tagName('a')).then(function(elem){

elem.click();
// Check url
WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
expect(browser.getCurrentUrl()).toContain('/source/');
});
});

关于javascript - Protractor 测试 chrome 不会点击项目,而 safari 会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31615886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com