我正在尝试在一个测试脚本中将Page对象与多个浏览器实例一起使用。.我有一个解决方案,但我正在寻找更好的解决方案。
var facebook=function(browserInstance){
browserInit=browserInstance;
element=browserInit.element;
this.email=function(){
return element(By.id("email"));
}
this.password=function(){
return element(By.id("pass"));
}
this.submit=function(){
return element(By.xpath("//input[@aria-label='Log In']"));
}
}
module.exports=facebook;
this.When(/^User entered the text in the email field$/,async function(){
// for using in browser2 i need to initialize it
page2=new facebook(browser2);
console.log(await page2.title()+" browser2");
await page2.search().sendKeys("testing");
browser2.sleep(3000);
//for using in first browser i need to initialize it
page1=new facebook(firstBrowser);
console.log(await page1.title()+" browser1")
await page1.email().sendKeys(testData.Login.CM[0].Username);
screenshots.takeScreenshot("newScreenshot");
firstBrowser.sleep(5000);
//now if i want again in browser2.. i need to initialize the page2 with browser instance again like below
page2=new facebook(browser2);
console.log(await page2.title()+" browser2");
});
如何每次都克服这种初始化
答案 0 :(得分:0)
我认为您可以在conf.js
文件中执行类似的操作
baseUrl: 'http://localhost:8081',
onPrepare: function() {
global.browsers = {
a: browser,
b: browser.forkNewDriverInstance()
};
}
在spec文件或步骤定义文件中,您可以拥有
this.When(/^User entered the text in the email field$/,async function(){
let browser1 = browsers.a;
let browser2 = browsers.b
//do your stuff
.
.
.
.
.
});