Selenium webdriver 在具有非角度页面的WebDriver中切换窗口和焦点

Selenium webdriver 在具有非角度页面的WebDriver中切换窗口和焦点,selenium-webdriver,protractor,Selenium Webdriver,Protractor,我有两个无角度的页面,其中一个有一个表单,我可以输入文本,一旦我点击Invoke按钮,我就会看到另一个浏览器窗口(在Chrome中显示为选项卡),其中有一些文本输出。新选项卡成为焦点有一段时间,但一旦我尝试获取新元素,它就会重新聚焦到第一个选项卡上。以下是我的测试结果: beforeEach(function () { browser.ignoreSynchronization = true; }); it('Do authentication of sessi

我有两个无角度的页面,其中一个有一个表单,我可以输入文本,一旦我点击Invoke按钮,我就会看到另一个浏览器窗口(在Chrome中显示为选项卡),其中有一些文本输出。新选项卡成为焦点有一段时间,但一旦我尝试获取新元素,它就会重新聚焦到第一个选项卡上。以下是我的测试结果:

beforeEach(function () {
        browser.ignoreSynchronization = true;
    });

    it('Do authentication of session', function () {
        session.goToCommandPage().then(function () {
            browser.findElement(by.css('#content > span:nth-child(3) > ul > li:nth-child(5) > a', 'AuthenticateUser')).click();
        }).then(function () {
            browser.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(3) > td:nth-child(2) > input')).sendKeys(COMPANY_ID);
        }).then(function () {
            browser.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > input')).sendKeys('ADMIN');
        }).then(function () {
            browser.findElement(by.buttonText('Invoke')).click();
        }).then(function () {
            browser.sleep(7000);
        })
    });

    it('Switch to the results tab', function () {
        browser.getAllWindowHandles().then(function (handles) {
            browser.getTitle().then(function (title) {
                console.log('Title: ' + title);
            });
            if (handles.length === 2) {
                browser.switchTo().window(handles[1]).then(function () {
                    browser.sleep(7000);
                }).then(function () {
                    browser.getTitle().then(function (title) {
                        console.log('Title now: ' + title);
                    });
                    browser.findElement(by.css('.collapsible .expanded .text')).getText().then(function (textProduced) {
                        console.log('Text printed: ' + textProduced);
                    });
                });
            }
        });
    });
正如您在上面所看到的,我尝试使用句柄,但在访问句柄[1]的地方,它不会直观地将选项卡更改为第二个选项卡。这段代码挂起在我得到句柄的地方。我看到标题打印出来了,它是第一个表单的标题,但它只是在打印出来之后超时,这给了我一个ScriptTimeoutError。我甚至试着向主体发送键,这确实改变了选项卡,但第二个选项卡的元素找不到。我正在使用Chrome和量角器来运行测试。我通常使用有角度的页面,但这两个页面不是有角度的页面,而是我稍后在有角度的页面中检查的内容

先谢谢你

编辑:我已经编辑了我的代码,以通过句柄,并根据页面标题进行更改。它似乎没有更改为另一个选项卡。关于我可能做错什么有什么建议吗?注意:这是用JavaScript编写的

it('Switch to the results tab', function () {
        browser.getAllWindowHandles().then(function (handles) {
            browser.getTitle().then(function (title) {
                console.log('Title: ' + title);
            });
            if (handles.length > 1) {
                handles.forEach(function (tab) {
                    browser.switchTo().window(tab).then(function () {
                        browser.sleep(7000);
                    }).then(function () {
                        browser.getTitle().then(function (title) {
                            console.log('Title now: ' + title);
                            if(title != 'WebService Web Service') {
                                browser.findElement(by.css('.collapsible .expanded .text')).getText().then(function (textProduced) {
                                    console.log('Text printed: ' + textProduced);
                                });
                            }
                        });
                    });
                });
            }
        });
    });

编辑:我用来传输到新打开的窗口的代码,但它会导致“ScriptTimeoutError:timeout:接收来自渲染器的消息时超时”


下面的代码有时会运行,但大多数情况下会失败

下面是修改后的代码:

describe('Test', function () {
    beforeEach(function () {
        browser.ignoreSynchronization = true;
    });

    it('Do authentication of session', function () {
        session.goToCommandPage().then(function () {
            browser.sleep(12000);
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span:nth-child(3) > ul > li:nth-child(5) > a'))
                    .then(function (elem) {
                        elem.click();
                        return true;
                    });
            }, 12000, 'Waited for the AuthenticateUser link to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(3) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.sendKeys(COMPANY_ID);
                        return true;
                    });
            }, 12000, 'Waited for the Company ID field to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.sendKeys('ADMIN');
                        return true;
                    });
            }, 12000, 'Waited for the User field to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(6) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.submit();
                        return true;
                    });
            }, 12000, 'Waited for the Invoke button to load');
        }).then(function () {
            browser.sleep(7000);
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.getAllWindowHandles().then(function (handles) {

                    if (handles.length > 1) {
                        return true;
                    }
                });
            }, 12000, 'Waited for window count to be greater than 1');
        });
    });

    it('Switch to the results tab', function () {
        browser.driver.getAllWindowHandles().then(function (handles) {
            browser.driver.getTitle().then(function (title) {
                console.log('Title: ' + title);
            });
            if (handles.length > 1) {
                handles.forEach(function (tab) {
                    browser.driver.switchTo().window(tab).then(function () {
                        browser.sleep(7000);
                    }).then(function () {
                        browser.driver.getTitle().then(function (title) {
                            console.log('Title now: ' + title);
                            if(title == 'localhost/Service/WebService.asmx/AuthenticateUser') {
                                browser.driver.findElement(by.css('.collapsible .expanded .collapsible-content .text')).getText().then(function (textProduced) {
                                    console.log('Text printed: ' + textProduced);
                                });
                            }
                        });
                    });
                });
            }
        });
    });
});

我不知道为什么我不能始终如一地通过

浏览器窗口/选项卡。如何按标题引用选项卡。第一个选项卡有一个正式的html标题元素,但下一个选项卡的标题区域中只有url(在chrome中),即localhost/Service/…如何通过标题引用选项卡?我一直在网上寻找一个例子,但到目前为止,我所发现的只是如何获得浏览器窗口的标题,正如我在上面所做的那样。它是一个窗口,而不是一个选项卡。浏览器窗口的标题是浏览器“选项卡”的标题。标签是Firefox、Chrome等提供的视觉效果。Selenium只关心HTML页面,它使用“窗口”一词来描述页面容器。要通过标题引用“选项卡”,请在切换到窗口后使用
driver.getTitle()
。要可靠地切换到一个新窗口(“选项卡”),您应该在打开窗口之前获取所有窗口句柄,然后获取所有窗口句柄,然后切换到“之后”列表中的句柄,而不是“之前”列表中的句柄。我尝试查看句柄并根据标题进行切换。似乎还是不起作用。(请参见上面的编辑)。这样看起来更好(注意:我从未使用过JSWebDriver)。您的
控制台.日志是否看起来发生了正确的事情?如果从重复的
browser.sleep(7000)
更改为初始的
browser.manage().timeout().pageLoadTimeout(7000)它会变得更好吗?
describe('Test', function () {
    beforeEach(function () {
        browser.ignoreSynchronization = true;
    });

    it('Do authentication of session', function () {
        session.goToCommandPage().then(function () {
            browser.sleep(12000);
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span:nth-child(3) > ul > li:nth-child(5) > a'))
                    .then(function (elem) {
                        elem.click();
                        return true;
                    });
            }, 12000, 'Waited for the AuthenticateUser link to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(3) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.sendKeys(COMPANY_ID);
                        return true;
                    });
            }, 12000, 'Waited for the Company ID field to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(4) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.sendKeys('ADMIN');
                        return true;
                    });
            }, 12000, 'Waited for the User field to load');
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.findElement(by.css('#content > span > form > table > tbody > tr:nth-child(6) > td:nth-child(2) > input'))
                    .then(function (elem) {
                        elem.submit();
                        return true;
                    });
            }, 12000, 'Waited for the Invoke button to load');
        }).then(function () {
            browser.sleep(7000);
        }).then(function () {
            browser.driver.wait(function () {
                return browser.driver.getAllWindowHandles().then(function (handles) {

                    if (handles.length > 1) {
                        return true;
                    }
                });
            }, 12000, 'Waited for window count to be greater than 1');
        });
    });

    it('Switch to the results tab', function () {
        browser.driver.getAllWindowHandles().then(function (handles) {
            browser.driver.getTitle().then(function (title) {
                console.log('Title: ' + title);
            });
            if (handles.length > 1) {
                handles.forEach(function (tab) {
                    browser.driver.switchTo().window(tab).then(function () {
                        browser.sleep(7000);
                    }).then(function () {
                        browser.driver.getTitle().then(function (title) {
                            console.log('Title now: ' + title);
                            if(title == 'localhost/Service/WebService.asmx/AuthenticateUser') {
                                browser.driver.findElement(by.css('.collapsible .expanded .collapsible-content .text')).getText().then(function (textProduced) {
                                    console.log('Text printed: ' + textProduced);
                                });
                            }
                        });
                    });
                });
            }
        });
    });
});