Ways to perform headless browser Testing with chrome


What is headless Testing

Headless Testing means running a browser UI test without the head aka. no browser UI. It is extremely helpful when we don’t really care about the UI, but would like to execute out Automation tests as fast as possible and not involving any “draw” operations onto the screen that consumes it own time and memory.


Ways to execute headless browser Testing
1.        
If Chrome version is 59 we can use ChromeOption class as suggested below
ChromeOptions options = new ChromeOptions();
//Adding "headless" argument to options
options.addArguments("headless");
driver = new ChromeDriver(options);


if Chrome version is less than 59 we can use HtmlUnitDriver which allows us to select version of browser that you would like to run your tests on. You can select the browser version like this.





How to enable javascript support in Headless driver.


Can we pass more than one parameter in sendKeys() method



==========Selenium sendKeys Method ==============

public class Links {

public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\selenium-java-3.5.0\\Browser Exe\\Chromedriver.exe");
WebDriver driver = new ChromeDriver();
//driver.manage().window().maximize();
driver.get("https://www.linkedin.com/");
driver.findElement(By.id("reg-firstname")).sendKeys("Test ", "Best ", "Rest ");
driver.close();
}
}

===================seendKeys() Method with parameter =========================



====================Explanation ================

 CharSequence represents an interface to a sequence of characters, with operations common to all classes implementing it. Mutability is not enforced by this interface, So, you can have an immutable implementing class, like String or mutable ones, like StringBuilder and StringBuffer.

Since CharSequence  is the parent interface for all these class thus we can cast all the sub classes into this type. so this is the reason behind accepting the Sting, so when we pass the string it will e automatically upcasted in to CharSequence.

and  ... (three dots) in Java known as "ellipsis", now varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don't know how many argument we will have to pass in the method, varargs is the better approach.

Always remember "Varargs" stands for Variable Argument .

The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:

return_type method_name(data_type... variableName){}

org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary - While Running Through Jenkins

[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ Maven.Final ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 32967
Only local connections are allowed.
FAILED CONFIGURATION: @BeforeClass setUp
org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 54 milliseconds
Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:01:39.354Z'
System info: host: 'XXXXXXXXXX', ip: 'XXXXXXXXXXX', os.name: 'Windows 10', os.arch: 'XXXXXX', os.version: '10.0', java.version: '1.8.0_131'
Driver info: driver.version: ChromeDriver
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)


Let see how to resolve this problem, if we run our project through intellj or eclipse using TestNG or Maven our Script will work fine but when we try to run this using Jenkins the Jenkins could not access the binary from our system, since It is possible that chrome is installed on your AppData folder in C drive, so we need to be sure that jenkins is able to access our chrome binary file.


Now to access the binary file we need to add below snippet


ChromeOptions chromeOptions= new ChromeOptions();
chromeOptions.setBinary("C:\\Users\\Manish\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
 System.setProperty("webdriver.chrome.driver","C:\\Users\\manish\\Downloads\\chromedriver_win32\\chromedriver.exe");
 driver = new ChromeDriver(chromeOptions);


i hope this resolve your problem let me know if this won't work for you guys.

Selenium Interview Questions and Answers

Selenium Interview Questions 


What is WebDriver?

What are the methods Present in the WebDriver interface?

What is the difference between get() and navigate.to() Method ?

WebDriver driver = new FirefoxDriver(); How does it work?

What are the reasons we don’t write “FirefoxDriver driver = new FirefoxDriver();”.

Do we have another way to enter text in to the text box without sendKeys() function?

Is WebElement is interface or a class?

What are the methods present in the WebElement interface ?

What is the return type of navigate method ?

Explain navigate().to() function. ?

How to find the count of total number of input box on the page ?

How to print the text present on the button ?

How to find the total number of buttons present on the page ?

How to find the width and height of the button ?

What error message would return if we pass wrong xpath expression?

How to find the x and Y coordinates for any web Element in selenium.

How to get typed text from a textbox ?

What is the super interface of Selenium ?

What all methods are present in super interface of selenium?

What are the different ways to click on the link?

How to find all the links present on the page?

How to find all the hidden links present on the page ?

How to find all the broken links present on the page ?

Explain list interface in java Collections ?

How to get the index of the object present in the list?

What is the difference between findElement and findElements?

What exception will you get if you don’t find any element on the page using findElement and findElements?

How to click all the links present on the page one by one ?

How to click on the hidden link present on the page ?

Difference between List and Set Interface ?

What is meant by ArraryList is not synchronized ?

Explain IsDisplayed method in Selenium ?

What is Random class in java?

Write a program for below mention scenario.

·         Let say we have 50 links on the page and every time I run the test I wanted to click a link randomly 

Write a program to find total number of radio button on the page ?

WAP to find selected radio button for rediff sign-up page ?

Create a function to get all the links present on the page.

Create a function to get list of all the visible links.

Create a function to get the list of link present in a specific div.

Create a function to click on the link based on user inputs.

Create a function which will click on any random link. (Random Class)

Create a function to get the herf attribute value of the links.

Create a function to select dropdown value using sendkeys function.

Create a function to select any value from dropdown using Select class.

Create a function to get the list of all the dropdown values.

Create a function to get the text of selected element present in the dropdown.

Create a function to get count of window instances.

Create a function to get count of frames present on the page.

Create a function to switch on desired window and close that pop-up window.

Create a function to check in Alert is present.

Create a function to get the text of alert and close the same.

Create a function to switch on the desired frame.

Which function we used to get back from frame to window.

Suppose we have two frames on a page 1 want to click ok button which is present on first frame and then would like to click on refresh button which Is present on the second frame write a code for the same.

What is the difference between quit and close function.

How to handle light boxes in selenium for reference use Flipkart login.

How to handle Java Alert

Create a custom function having return type as webElement based on any locator.

Create function to hover and click.

Create a program to use draganddrop functionality

Create a function to take the screenshots

Create a function to scroll down.

Create a function to scroll at a particular  element

Create a function to simulate right click of mouse

Create a function to store all the values for a particular column ?

Create a function to store all the values for a particular Row ?

What is meant by locators list out all the locator Names?

Create a OpenApplication function which include implicit wait as well as PageTimeOut.

What is the difference between Implicit wait and explicit wait?

ExpectedConditions  is a class or a interface ?

How to get the path till current working directory.

How to handle window based pop-up.

========================================================================
Question  :- What is the full form for ANT and POI ?

Answers :-  POI stands for :- “Poor Obfuscation Implementation” and Ant Stands for “Another Neat Tool”.

=======================================================================
Question :-  How do you Achieve Parallel execution In TestNG ?

Answers :- To run our test cases i.e methods or classes parallelly  using testNG we need to use Parallel attribute in TESTNG.xml file also need to specify how many instances you want to open in one go for parallel execution and this can be achieved via thread count attribute in TestNG.xm file.

Note:- parallel and thread-count attribute should always be defined at suite level.


=======================================================================

Question :- How to run failed test cases only using TestNG.

Answer:- To execute only failed tests we have provision in the TestNg. After completion of the suite execution; just refresh the project folder then you can find a folder called “test-output” and there you can find an xml file called “testng-failed.xml”. This xml file contains only failed test cases. So, we can execute this xml file to rerun the failed test again.

=======================================================================

Question :- How to rerun the failed test cases in TestNG with in the execution mode.

Answers :- we can execute failed test cases using TestNG in Selenium –By Implementing TestNG IRetryAnalyzer.


=======================================================================
Question :- What is CharSequence in Sendkeys?

Answer :- CharSequence represents an interface to a sequence of characters, with operations common to all classes implementing it. Mutability is not enforced by this interface, So, you can have an immutable implementing class, like String or mutable ones, like StringBuilder and StringBuffer.

Since CharSequence  is the parent interface for all these class thus we can cast all the sub classes into this type. so this is the reason behind accepting the Sting, so when we pass the string it will e automatically upcasted in to CharSequence.

=======================================================================

Question :- Did you noticed that we have ... (three dots) with CharSequence present under sendKeys what does they signify ?

Answer :- These ... (three dots) in Java known as "ellipsis", now varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don't know how many argument we will have to pass in the method, varargs is the better approach.

Always remember "Varargs" stands for Variable Argument .

The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:

return_type method_name(data_type... variableName){}

========================================================================

Question : Can we pass more than one parameter in sendKeys() method.

Answer :- Yes we can, then reason is sendKeys method defines as below.

public String sendKeys(CharSequence ... args0)
{
}

Example  :- here

=======================================================================

Question :- How to test the change in color of any hyper link after hovering the link by using selenium.

Answer :- we can use method .GetCSSValue("color") on a webElement to get its color value.

Example :-

 String headerColor = driver.findElement(By.xpath(".//*[@id='LoginName']/h1")).getCssValue("Color");
   Assert.assertEquals("some message", "#EEEEEE", Color);

=======================================================================