因此,我在一个react项目中使用selenium-webdriver运行selenium测试。每次我运行测试时,它都会打开一个新的铬窗口,这非常令人恼火,因为我最终打开了一百万个铬窗口。是否可以强制selenium使用已经打开的浏览器窗口?
编辑:这里是测试代码的一个简单示例。
const webdriver = require('selenium-webdriver');
const { By, Key } = webdriver
describe('Dashboard page', () => {
it('renders correctly', async() => {
var chromeCapabilities = webdriver.Capabilities.chrome();
var chromeOptions = {
//'args': ['--headless']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
const driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
await driver.get('http://localhost:3000/dashboard')
await driver.getTitle().then((title) => {
expect(title).toEqual("My website | Dashboard")
})
await driver.getCurrentUrl().then((url) => {
expect(url).toEqual("http://localhost:3000/dashboard")
})
})
})
发布于 2018-09-06 02:17:35
如果您使用javascript绑定和Jasmine框架,那么您可以尝试使用下面的代码。您还可以参考jasmin文档获得更多详细信息( 这里 )。
beforeEach只对spec.js中的所有测试运行一次
在beforeEach中启动浏览器会话
afterEach将为spec.js中的所有测试运行一次
AfterEach中的终端浏览器会话
describe('Before Each Spec', function () {
beforeEach(function () {
// Create new browser instance once for all spec tests
var chromeCapabilities = webdriver.Capabilities.chrome();
var chromeOptions = {
//'args': ['--headless']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
const driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
});
describe('Test Method 1', function() {
it('should have a title', function() {
// TO DO Code
});
});
describe('Test Method 2', function() {
it('should have a something to test', function() {
// TO DO Code
});
});
describe('After Each Spec', function () {
afterEach(function () {
// Destroy browser after all tests finished
browser.quit(); (or browser.driver.close();)
});
如果您正在使用java,那么您可以使用下面的注释,它只对完整的testng xml运行一次,或者对每个testng类运行一次,例如@BeforeSuite或@BeforeClass
@BeforeSuite
public void setUP(){
startSeleniumSession();
}
public void startSeleniumSession(){
WebDriver driver = new ChromeDriver();
}
@Test
public void startTest2(){
driver.get("some url 1");
driver.findElement(By.id("someID")).click()
}
@Test
public void startTest2(){
// this test will run in same browser
driver.get("some url 2");
driver.findElement(By.id("someID")).click()
}
@AfterSuite
public void tearDown(){
driver.quit();
}
发布于 2019-02-21 02:21:50
此设置适用于我:
options = Options()
options.add_argument('--headless')
options.add_argument('--profile-directory=Default')
browser = webdriver.Chrome(options=options,executable_path='./chromedriver/chromedriver.exe')
关键是要建立:
options.add_argument('--profile-directory=Default')
https://stackoverflow.com/questions/52201724
复制相似问题