0

Selenium grid test case failed in headless browser mode only

Normal Browser Mode [Test Success]

  • selenium stand alone server : 3.0.1
  • Firefox : 38.0.1
  • ip : 192.168.1.40

Headless Browser Mode [Test Failed]

  • selenium stand alone server : 3.0.1
  • Firefox : 38.0
  • ip : 172.18.0.60

I run headless browser like below

xvfb-run java -Dwebdriver.firefox.marionette="/tmp/geckodriver" -Dwebdriver.firefox.bin="/tmp/firefox/firefox" -jar selenium-server-standalone-3.0.1.jar -role webdriver -hub http://192.168.1.106:4444/grid/register -port 5566 -host 172.18.0.60

Test Case Code:

package example;

import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;

public class NewTest
{
   public WebDriver driver;
   public String URL, Node;
   private JavascriptExecutor jse;
   private String baseUrl;

   protected ThreadLocal<RemoteWebDriver> threadDriver = null;

   @Parameters({"browser","url"})
   @BeforeTest
   public void launchapp(String browser,String url) throws MalformedURLException
   {
      baseUrl = "http://myhost";

      if (browser.equalsIgnoreCase("firefox"))
      {
         System.out.println(" Executing on FireFox");
         String Node = url;
         DesiredCapabilities cap = DesiredCapabilities.firefox();
         cap.setBrowserName("firefox");

         driver = new RemoteWebDriver(new URL(Node), cap);         
         // Puts an Implicit wait, Will wait for 10 seconds before throwing exception
         driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

         // Launch website
         driver.navigate().to(baseUrl);
         driver.manage().window().maximize();
      }          
      else
      {
         throw new IllegalArgumentException("The Browser Type is Undefined");
      }

      jse = (JavascriptExecutor)driver;
   }

   @Test
   public void functionalityTest() throws InterruptedException
   {      
       driver.findElement(By.linkText("Sign In")).click();
       driver.findElement(By.id("email")).clear();
       driver.findElement(By.id("email")).sendKeys("xxxxx@xxx.xxx");
       driver.findElement(By.id("pass")).clear();
       driver.findElement(By.id("pass")).sendKeys("xxxxxx");
       driver.findElement(By.id("send2")).click();    
       jse.executeScript("scroll(0, 250);");
       driver.findElement(By.cssSelector(".customerlink a")).click();
       Thread.sleep(1000);
       driver.findElement(By.linkText("My Account")).click();    
       Thread.sleep(2000);
       jse.executeScript("scroll(0, 250);");
       Thread.sleep(1000);
       jse.executeScript("scroll(0, -250);");
       Thread.sleep(2000);
       driver.findElement(By.cssSelector(".customerlink a")).click();
       Thread.sleep(1000);
       driver.findElement(By.linkText("Sign Out")).click();
       Thread.sleep(2000);  
   }

   @AfterTest
   public void closeBrowser()
   {
      driver.quit();
   }
}

I got exception in very first line of headless brwoser mode

07:16:53.571 INFO - Executing: [new session: Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]])
07:16:53.580 INFO - Creating a new session for Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]
07:17:00.268 INFO - Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
07:17:02.648 INFO - Detected dialect: OSS
07:17:02.665 INFO - Done: [new session: Capabilities [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, version=, platform=ANY}]]
07:17:02.701 INFO - Executing: [implicit wait: 50000])
07:17:02.717 INFO - Done: [implicit wait: 50000]
07:17:02.724 INFO - Executing: [get: http://myhost])
07:18:03.564 INFO - Done: [get: http://myhost]
07:18:03.570 INFO - Executing: [maximise window])
07:18:03.580 INFO - Done: [maximise window]
07:18:03.593 INFO - Executing: [find element: By.linkText: Sign In])
07:18:53.917 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Sign In"}

like

The error state clearly unable to locate element but how it worked in normal mode?

Is there any restriction in code to use headless browser mode?

Bilal Usean
  • 2,322
  • 3
  • 22
  • 45
  • May i know where in the above, you are using "headless browser" concept? As far as i know, headless browser is implement in "HtmlUnitDriver" and may be NodeJSDriver(this i didnt tried yet). Am i missing any concept here? – Uday Feb 27 '17 at 07:05
  • buddy, me too new!. I believe headless mode is run selenium test without Browser UI. My browser run on xvfb instead of UI mode so I believe it is headless mode. for your easy reference http://agiletesting.blogspot.in/2016/01/running-selenium-webdriver-tests-using.html – Bilal Usean Feb 27 '17 at 07:32
  • Seems like this xvfb works in Linux/Ubuntu environment and not on Windows. Found xnest is also for Linux/Ubuntu env. Found this link 'http://stackoverflow.com/questions/944086/is-there-anything-like-xvfb-or-xnest-for-windows' for windows version. – Uday Feb 27 '17 at 07:47

1 Answers1

0

Finally found, Headless browser does not take account of driver.manage().window().maximize(); so I set screen size through xvfb-run command

-s "-screen 0, 1920x1080x24"

Example:

xvfb-run -s "-screen 0, 1920x1080x24" java -Dwebdriver.firefox.marionette="/tmp/geckodriver" -Dwebdriver.firefox.bin="/tmp/firefox/firefox" -jar selenium-server-standalone-3.0.1.jar -role webdriver -hub http://192.168.1.106:4444/grid/register -port 5566 -host 172.18.0.60

Info: I'm using bootstrap so visible of element to be changed according to the screensize. So it will throw element not found exeception

Bilal Usean
  • 2,322
  • 3
  • 22
  • 45