【问题标题】:How to prevent selenium to open a new chrome window on every test run?如何防止 selenium 在每次测试运行时打开一个新的 chrome 窗口?
【发布时间】:2019-02-11 13:53:20
【问题描述】:

所以我在 react 项目中使用 selenium-webdriver 运行 selenium 测试。每次我运行测试时,它都会打开一个新的 chrome 窗口,这非常令人恼火,因为我最终打开了一百万个 chrome 窗口。是否可以强制 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")
    })
  })
})

【问题讨论】:

  • 您能否向我们展示您的代码,它启动了 chrome 并关闭了浏览器。基本上,您目前已经编写了代码,它在每次运行时都会打开一个浏览器,但您没有任何浏览器拆解。
  • 对,这是有道理的。我添加了一个小代码示例。是的,我每次运行时都会打开一个新浏览器。我想知道是否有办法告诉 selenium 使用任何当前打开的浏览器窗口而不是打开一个新窗口。
  • 如果您关心重用现有的浏览器会话(cookie、存储 + 可能是 chrome 配置文件)然后检查这些链接 --> stackoverflow.com/questions/8344776/… + stackoverflow.com/questions/47861813/… 因为我很确定它们可能会有所帮助这个主题(虽然使用 JavaScript)。
  • @Wazniak 谢谢!非常有用的链接

标签: selenium selenium-webdriver


【解决方案1】:

此设置对我有用:

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') 

【讨论】:

    【解决方案2】:

    如果您在 Jasmine 框架中使用 javascript 绑定,那么您可以尝试使用以下代码。您也可以参考 jasmin 文档了解更多详情 here

    对于 spec.js 中的所有测试,beforeEach 只会运行一次

    在 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();
    }
    

    【讨论】:

    • 我认为是driver.quit() 在这里工作?虽然我不明白这如何解决问题,但我明白你的意思是关闭窗口并至少在每次测试运行时只打开一个。我希望能够打开窗口并重复使用那个窗口。
    • @Majoren - 我已经更新了 javascript 绑定的代码。想法是将浏览器创建会话代码保留在一个块(beforeEach)中,该块将只执行一次。所以新的浏览器不会打开,所有的测试都会在同一个浏览器中运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多