Search code examples
google-chromeseleniumheadless

Outlook/Yahoo behave weirdly when running selenium tests headless within Chrome


This is really strange. Tried with Yahoo, and now Outlook. I'm running selenium tests which send an email from the application I'm testing. I want to assert the test by checking the correct email is received. Once email has been sent, I start a second instance of chrome driver and open a test email account I've set up (Yahoo/Outlook), sign in, pick up the email, do some assertions, test passed.

However, as soon as I try and run the test in headless mode, it's like the version of Outlook is different, so the element is not found, the test fails. Run it out of headless, works fine. Its using the same version of chrome, chrome driver, and the same chrome profile. I can see the outlook web page looks different by getting my code to take a screenshot on failures, so I can see where things are failing even when I'm running in headless.

Has anyone ever experienced problems like this before, and if so, how the blazes do I fix it? It's doing my swede in now.


Solution

  • It seems that, at least for outlook, Microsoft is testing which browser you use. At least this is what I am facing. Since I get, as you do, a different page when I use "headless" option of my chromedriver in Selenium. I fixed if by setting the user agent of my ChromeDriver :

    System.setProperty("webdriver.chrome.driver", prop.getProperty("driver.path"));
    ChromeOptions chromeOptions  = new ChromeOptions();
    chromeOptions.addArguments("headless");
    chromeOptions.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36");
    ChromeDriver driver = new ChromeDriver(chromeOptions);