So I'm running selenium tests with selenium-webdriver in a react project. Every time I run the tests it opens up a new chrome window, which is extremely irritating, since I end up with a million chrome windows open. Is it possible to force selenium to use the browser window already open?
EDIT:
Here's a simple example of the test code.
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")
})
})
})
Best Answer
If you are using javascript bindings with Jasmine framework then you can try using below code. You can also refer jasmin docs for more details here
beforeEach will run only once for all tests inside a spec.js
afterEach will run once for all tests inside a spec.js
If you are using java then you can use below annotation which runs only once for complete testng xml or once per testng class e.g. @BeforeSuite or @BeforeClass