Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging with headless browser

I have a WebDriver testsuite, which operates different when I execute it in normal and headless browser. There is an element which is not found when I execute it in headless mode, but found when I use the same code, same driver in normal mode. I use this flag to set headless mode:

chromeOptions.addArguments("--headless");

There is ChromeDriver 2.31 and WebDriver 3.5.2 in use. How could I debug this?

like image 787
plaidshirt Avatar asked Sep 02 '17 20:09

plaidshirt


People also ask

What is a headless browser?

There are a number of headless browser options, but the one I’ll focus on is the headless version of Google Chrome. You can execute and run Google Chrome without the graphical user interface.

What is headless browser testing in selenium?

Headless Browser Testing is a way of running browser tests without the UI (head, a term from Unix). Let’s have a look at a Selenium test execution in headless mode as well as a normal mode for comparison. Firstly, normal selenium test execution with browser UI. Then the same test is run in headless mode.

What is the best way to run headless testing on Chrome?

If you are familiar with Node.js, you can run headless Chrome with Puppeteer relatively easily. Another option is Selenium, which is a popular solution for automated browser testing. By default, Selenium is not completely headless but can be configured to operate that way.

How do I Start Chrome in headless mode?

The easiest way to get started with headless mode is to open the Chrome binary from the command line. If you've got Chrome 59+ installed, start Chrome with the --headless flag: Note: Right now, you'll also want to include the --disable-gpu flag if you're running on Windows. See crbug.com/737678. chrome should point to your installation of Chrome.


2 Answers

There are two ways to debug. You can get Page Source and check what is different.

Now when you launch a browser using Selenium, it is using the Debugging session to automate chrome. So you can't do a remote debugger to your website using this.

You need to launch chrome manually.

chrome --headless --remote-debugging-port=9222 --disable-gpu http://tarunlalwani.com

Now in open another chrome and debug the site by going to http://127.0.0.1:9222 and inspect the site.

Debugging Session

like image 200
Tarun Lalwani Avatar answered Sep 22 '22 18:09

Tarun Lalwani


for debugging headless, try to get an screenshot before the error:

in Python:

WINDOW_SIZE = "1200,900" opts.add_argument("--window-size=%s" % self.WINDOW_SIZE) 
if self.HEADLESS:   opts.add_argument('--headless')     
self.driver = webdriver.Chrome(executable_path=chromedriver,options=opts)  


 driver.save_screenshot('./foto.png')
like image 20
Rogelio Triviño Avatar answered Sep 21 '22 18:09

Rogelio Triviño