Javascript 如何测试从cypress中现有选项卡重定向的新选项卡?
需要测试从现有选项卡重定向的新选项卡。我们怎样才能在cypress中测试它呢?你不能测试它。Cypress不支持新窗口和/或选项卡。作为解决办法,您可以做的是: 测试是否存在指向新窗口/选项卡的链接 创建另一个测试,立即打开该窗口/选项卡的URL。Javascript 如何测试从cypress中现有选项卡重定向的新选项卡?,javascript,cypress,Javascript,Cypress,需要测试从现有选项卡重定向的新选项卡。我们怎样才能在cypress中测试它呢?你不能测试它。Cypress不支持新窗口和/或选项卡。作为解决办法,您可以做的是: 测试是否存在指向新窗口/选项卡的链接 创建另一个测试,立即打开该窗口/选项卡的URL。 你不能对此进行测试。Cypress不支持新窗口和/或选项卡。作为解决办法,您可以做的是: 测试是否存在指向新窗口/选项卡的链接 创建另一个测试,立即打开该窗口/选项卡的URL。 如果它被重定向到一个新的url/选项卡,即一个新的子域,并且在cypre
你不能对此进行测试。Cypress不支持新窗口和/或选项卡。作为解决办法,您可以做的是: 测试是否存在指向新窗口/选项卡的链接 创建另一个测试,立即打开该窗口/选项卡的URL。 如果它被重定向到一个新的url/选项卡,即一个新的子域,并且在cypress中作为一个新选项卡打开,那么您仍然可以使用cypress进行测试。 我以邮递员网站为例来说明这一点。在“邮递员”网站中,单击“学习中心”选项卡将打开一个新选项卡。下面的测试示例将在现有的测试运行程序选项卡中打开。请参考以下我的代码:
context('New tab actions', () => {
it('Test to open a new tab in cypress and test the page', () => {
cy.visit('https://www.getpostman.com/');
cy.get('#mobile-menu-button').click();
cy.get('#mobileNav > #siteNav > nav > ul > li').contains("Product ");
cy.get('#mobileNav > #siteNav > .site-nav__secondary-menu > a').first().then(($a)=>{
const newUrl = Cypress.$($a).attr('href');
console.log(newUrl);
cy.visit(newUrl);
cy.get('.header__title').invoke('text').then((text)=>{
const headerText = text;
expect(headerText).to.contain('Learning Center');
//Similarly you can follow the same mechanism and start writing all of remaining tests related to that page....
})
})
})
})
如果它被重定向到一个新的url/选项卡,即一个新的子域,并且在cypress中作为一个新选项卡打开,那么您仍然可以使用cypress进行测试。
我以邮递员网站为例来说明这一点。在“邮递员”网站中,单击“学习中心”选项卡将打开一个新选项卡。下面的测试示例将在现有的测试运行程序选项卡中打开。请参考以下我的代码:
context('New tab actions', () => {
it('Test to open a new tab in cypress and test the page', () => {
cy.visit('https://www.getpostman.com/');
cy.get('#mobile-menu-button').click();
cy.get('#mobileNav > #siteNav > nav > ul > li').contains("Product ");
cy.get('#mobileNav > #siteNav > .site-nav__secondary-menu > a').first().then(($a)=>{
const newUrl = Cypress.$($a).attr('href');
console.log(newUrl);
cy.visit(newUrl);
cy.get('.header__title').invoke('text').then((text)=>{
const headerText = text;
expect(headerText).to.contain('Learning Center');
//Similarly you can follow the same mechanism and start writing all of remaining tests related to that page....
})
})
})
})
对于这种情况,我确实找到了一个解决办法,我想访问一个新的URL,然后测试它的内容。这对我来说是有效的,因为URL在HTML中可用 如果有用的话,试试这个
it('redirect',()=>{
cy.visit('website')
cy.get(`.nav-items :nth-child(1) a`) // directly clicking this element opens the same link a new tab
.should('be.visible')
.invoke('attr','href')
.then(href=>{
cy.visit(href)
})
})
对于这种情况,我确实找到了一个解决办法,我想访问一个新的URL,然后测试它的内容。这对我来说是有效的,因为URL在HTML中可用 如果有用的话,试试这个
it('redirect',()=>{
cy.visit('website')
cy.get(`.nav-items :nth-child(1) a`) // directly clicking this element opens the same link a new tab
.should('be.visible')
.invoke('attr','href')
.then(href=>{
cy.visit(href)
})
})
您能检查重定向是否发生在主url下的新子域上吗?例如,如果主url如下所示https://www.somesite.com.au/ 并重定向到一个新的url,类似这样>>https://business.somesite.com.au/,如果是,那么我可以帮你…你能检查重定向是否发生在主url下的新子域上吗?例如,如果主url如下所示https://www.somesite.com.au/ 并重定向到一个新的url,类似这样>>https://business.somesite.com.au/,如果是的话,我可以帮你。。。