2
votes

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?

enter image description here

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")
    })
  })
})
2
Could you show us your code which starts the chrome and which should close the browser. Basically you currently have coded that it opens a browser on each run, but you don't have any browser teardown. - Madis Kangro
Right, that makes sense. I added a little code sample. Yes, I do open a new browser on every run. I was wondering if there is a way to tell selenium to use any currently open browser window instead of opening a new one. - Majoren
If you care about reusing existing browser session (cookie, storage + maybe chrome profile) then check those links --> stackoverflow.com/questions/8344776/… + stackoverflow.com/questions/47861813/… cause i'm pretty sure they may be helpful in this topic (although using JavaScript). - Wazniak
@Wazniak Thank you! Very helpful links - Majoren

2 Answers

2
votes

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

Start browser session in beforeEach

afterEach will run once for all tests inside a spec.js

End browser session in 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();)

  });

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

@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();
}
0
votes

This settings worked for me:

options = Options()
options.add_argument('--headless')
options.add_argument('--profile-directory=Default') 
browser = webdriver.Chrome(options=options,executable_path='./chromedriver/chromedriver.exe')

Key thing is to set up:

options.add_argument('--profile-directory=Default')