4

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")
    })
  })
})
Majoren
  • 983
  • 5
  • 16
  • 36
  • 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 Sep 06 '18 at 10:14
  • 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 Sep 06 '18 at 11:32
  • 1
    If you care about reusing existing browser session (cookie, storage + maybe chrome profile) then check those links --> https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session + https://stackoverflow.com/questions/47861813/how-can-i-reconnect-to-the-browser-opened-by-webdriver-with-selenium cause i'm pretty sure they may be helpful in this topic (although using JavaScript). – Wazniak Sep 06 '18 at 12:52
  • @Wazniak Thank you! Very helpful links – Majoren Sep 06 '18 at 13:08

2 Answers2

2

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();
}
Amit Jain
  • 4,389
  • 2
  • 18
  • 21
  • 1
    I assume it's the `driver.quit()` doing the job here? I don't see how this solves the issue though, but I get your point in closing the window and at least opening only one on every test run. I want to be able to have the window open and just reuse that one. – Majoren Sep 06 '18 at 11:30
  • @Majoren - I have updated the code for javascript binding. Idea is to keep browser creation session code in a block (beforeEach) which will execute only once. So new browser will not open,and all tests will run in same browser. – Amit Jain Sep 06 '18 at 12:02
0

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') 
Hrvoje
  • 13,566
  • 7
  • 90
  • 104