简体   繁体   中英

Testing with a selenium driver in Java without opening any browser

I need to test with selenium chrome driver in Java. But chrome window should't be opened. Assume this a product and no window should be opened.

I've also looked at this one; Is it possible to hide the browser in Selenium RC? But no solution for me. The testing should be operating system independent and I've tried HtmlUnitDriver for testing without opening any window but it has some problem. When there is finding components by id, it may not find the component by id. Some servers may send the component id according to browser and I can't know what id I should use to test.

Because of that I'm trying to use chrome driver.

Is there a way to use chromedriver without opening chrome window or another way to test without opening any window with Selenium in Java?

Thank!

Go with PhantomJS but if running them in chromedriver is required and you have the resources, this blog has a good recipe on running headless selenium with chrome . Requiring you to download the following...

  • VirtualBox
  • Vagrant
  • NodeJS

If you plan to implement Jenkins or any other CI in the future, I strongly suggest going with PhantomJS though.

In selenium web driver there is headless mode. so in headless mode you can do the automation without opening the web browser. and also you can deploy your application in none gui system

    ChromeOptions options = new ChromeOptions();
    // setting headless mode to true.. so there isn't any ui
    options.setHeadless(true);

    // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver(options);

GhostDriver and PhantomJS should let you do what you want.

I like this article .

Basically you need to add PhantomJS dependency in pom (I like maven for dependency management):

<dependency>
    <groupId>com.github.detro.ghostdriver</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.1.0</version>
</dependency>

And run code

    System.setProperty( "phantomjs.binary.path", "c:\\path\\to\\phantomjs-1.9.8-windows\\phantomjs.exe" );
    WebDriver driver = new PhantomJSDriver();
    driver.get("http://www.google.com");
    driver.quit();

It worked for me with versions:

  • PhantomJS 1.9.8
  • PhantomJS driver 1.1.0
  • Selenium 2.44.0

with Java you can use JSOUP library and then, using selenium to inspect the document.

Document doc = Jsoup.connect("PUT THE LINK")
                  .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36")
                  .get();

then, using selenium, you could make something like: doc.getElementByClassName etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM