Javascript 如何在量角器的两个窗口之间比较两个元素的值?
我需要保存“$$”[title=“Ir al documento”]').first().getText();”的值,以便在另一个窗口中进行比较Javascript 如何在量角器的两个窗口之间比较两个元素的值?,javascript,angularjs,variables,jasmine,protractor,Javascript,Angularjs,Variables,Jasmine,Protractor,我需要保存“$$”[title=“Ir al documento”]').first().getText();”的值,以便在另一个窗口中进行比较 it('spec', function () { var text; $$('#links-list a').first().click().then(function () { browser.driver.getAllWindowHandles().then(function (handles) {
it('spec', function () {
var text;
$$('#links-list a').first().click().then(function () {
browser.driver.getAllWindowHandles().then(function (handles) {
browser.driver.switchTo().window(handles[1]).then(function () {
browser.wait(EC.visibilityOf($('#introBienvenida .boton')), 5000);
$('#introBienvenida .boton').click();
element(by.id("texto")).sendKeys('texto refundido');
element(by.id("buttonSearch")).click();
text = $$('[title="Ir al documento"]').first().getText(); // I need save to compare this value.
$$('[title="Ir al documento"]').first().click();
});
browser.driver.close();
browser.driver.switchTo().window(handles[0]); // Back again.
});
});
expect($('#adDoc0 .AD-objetivo').getText()).toEqual(text); // I need assert the content of text
});
编辑:
我需要的是比较句柄为1的窗口中元素的值和句柄为0的窗口中元素的值
首先,我必须单击带有句柄0的窗口元素,然后我单击带有句柄1的窗口中的文档,得到一个值,然后我必须返回带有句柄0的窗口以获得一个值,并与带有句柄1的窗口的值进行比较。我想你需要带着承诺传递它
$$('[title="Ir al documento"]').first().getText().then(function(documentTitleText){
browser.driver.switchTo().window(handles[0]);
expect($('#adDoc0 .AD-objetivo').getText()).toEqual(documentTitleText);
});
量角器是根据承诺和回调的原则开发的
getText()
函数返回带有文本值的承诺。处理这种情况有很多方法
getText()
函数的承诺中期望它。(这一条已经被@user2020347发布为答案)var text;
$$('#links-list a').first().click().then(function () {
//switch to window 1
$$('[title="Ir al documento"]').first().getText().then(function(windowOneText){
text = windowOneText;
});
//close the windows to go back to first window
});
expect($('#adDoc0 .AD-objetivo').getText()).toEqual(text);
var text;
$$('#links-list a').first().click().then(function () {
//switch to window 1
$$('[title="Ir al documento"]').first().getText().then(function(windowOneText){
text = windowOneText;
});
//close the windows to go back to first window
return text;
}).then(function(argText){
expect($('#adDoc0 .AD-objetivo').getText()).toEqual(argText);
});
希望有帮助。我需要的是比较带句柄1的窗口中元素的值和带句柄0的窗口中元素的值。使用选项2解决。我忘了getText()返回承诺。谢谢