Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 15 September 2018

Why in Selenium findElements is made to return List and not Set

First of all let us see what are the difference between List and Set in Java.

Both are Interfaces extending Collections interface.

Some of the most used implementations of List in Java are ArrayList, LinkedList.
Some of the most used implementations of Set in Java are HashSet, LinkedHashSet, TreeSet.

These are the characteristics of List:
* Is a dynamic array.
* Any elements in the list can be controlled using index.
* Allows duplicate values.
* Allows null values.

These are the characteristics of Set:
* Is a dynamic array.
* There is no index to control elements in Set.
* Does not allow duplicate values. If duplicate entry added, will ignore it.
* Some implementations such as HashSet and LinkedHashSet allow null values, whereas some implementations like TreeSet do not allow null values.

Now, In Selenium why driver.findElements() made to return List and not Set. Is because, only List allows duplicate values and not Set. There is a possibility that when we driver.findElements() there could be duplicate elements returned and as well sometimes no elements returned (null). So list is the best possible return type for findElements method.

Similarly, why driver.getWindowHandles(); is made to return Set. Is because, every window will have unique window handle and cannot be duplicate and cannot be null.

That is the reason why driver.findElements()  is made to return List and driver.getWindowHandles(); is made to return Set. 

Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/

Monday, 10 September 2018

What is the difference between switchTo().defaultContent() and switchTo().parentFrame() in Selenium


switchTo().defaultContent() - will switch back to main HTML.

Where as, switchTo().parentFrame() switches to the immediate parent frame.

Let us understand through example. Below is the sample structure of html where frames are embedded inside each other. frame2 is available inside frame1 and frame3 available inside frame2.
frame3 contains dropdown1 and frame4 contains dropdown2.

frame1
     frame2
         frame3
                   select dropdown1
         frame4
                   select dropdown2



Now to work on first dropdown and then second dropdown this would be the code.

driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame3");
// select dropdown1 code

driver.switchTo().parentFrame(); // switched to frame2 frame
driver.switchTo().frame("frame4");
// select dropdown2 code

if switchTo().parentFrame();  not used, this would be the code:

driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame3");
// select dropdown1 code

driver.switchTo().defaultFrame(); // switched to main html
driver.switchTo().frame("frame1");
driver.switchTo().frame("frame2");
driver.switchTo().frame("frame4");
// select dropdown2 code

So this way, by using switchTo().parentFrame() it saves few lines of code compared to using defaultFrame() all the time.


Hope this blog has been useful to you.

Youtube channel: https://www.youtube.com/user/srinivaskinik
Facebook page: https://www.facebook.com/srinivaskinikalmady/


Monday, 3 September 2018

How to externalize key value pairs to a properties file using shortcut method

Let us say that, you have automation script in which you want externalize URL and other attribute to a properties file.







One way to do is, write a Java class that will read properties file and then have the method replacing hard coded value that will read it from properties file.

Shortcut for this is,

Right click on empty area in eclipse, and select Source --> Externalize Strings...
















In the window that is displayed, check all the values that you want to move to a properties file and rename key if required.

























Select 'Next' and you will see automatic Java code that will be generated.

And finally 'Finish'.

























Now, in the code, you will see, value has been moved to a properties file. Also you could see properties file, and class that was created.














Hope this blog has been useful to you.


Sunday, 2 September 2018

How to validate constructed xpath or CSS selector in Mozilla firefox / Google chrome without using any plugin



Let us say, for application under test, you have constructed relative xpath / CSS to be used in Selenium code.

Now you want to test or validate in browser without using any plugin.
This can be achieved using developer tools of Mozilla firefox. By going to Console tab, type

$x("provide your xpath");

for eg: $x("//*[@id='lst-ib']") and press enter. If it was correct locator, you will get the element returned in the console, which will highlight when hover on the web page.

Similarly to test, CSS in Mozilla firefox use $$("provide your css selector");
$$("input[id='lst-ib']")





In the Chrome browser, to test xpath / CSS, simply go to developer tools, and use Control +F and paste your xpath / CSS. You will get element highlighted in Yellow color



Hope this blog has been useful to you. Do comment if you have any further questions.

Saturday, 1 September 2018

How to get portion of text what you expect to verify in Selenium - Java

Let us take a scenario in which you need to verify number of records displayed in the page.



So from the web page text - 2 products found. We want to extract only 2. This can be achieved in below steps.

1) Write xpath to get text from web page,

String result_Text = driver.findElement(By.xpath("/html/body/table[5]/tbody/tr/td")).getText();

Note: In this case, used getText() because text is in html and not in attribute. If it is in attribute, you could use getAttribute();

2) Use indexOf method in java, that would return the position of particular string or character something like this:

int indexOfColon = result_Text.indexOf(':');

Similarly get index of products

int indexOfProducts = result_Text.indexOf('products');

3) Now use, subString() method in Java to get number what you are looking for:

String textReturned = result_Text.substring(indexOfColon+1, indexOfProducts).trim();

Now, textReturned is containing only number of products you are looking for.

You can use, Integer.parseInt(textReturned) to convert String value to int datatype.

Hope this blog has been useful to you. 

Writing xpath when there is apostrophe in text and use in Selenium

We usually construct relative xpath using //tagName[@attribute='value']
Note value is surrounded with single quote.

Suppose there is a special character like "'" (apostrophe) in string. For eg:

"What We're Reading"

Then, when xpath is constructed like this -

WebElement findElement = driver.findElement(By.xpath("//h1[text()='What We're Reading']"));

This exception would be thrown in Selenium:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //h1[text()='What We're Reading'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//h1[text()='What We're Reading']' is not a valid XPath expression.
  (Session info: chrome=68.0.3440.106)


To fix this error, you will have to redefine xpath something like this:

WebElement findElement = driver.findElement(By.xpath("//h1[text()=\"What We're Reading\"]"));

That is surrounding text with double quote in xpath and providing backslash character before ".


Friday, 10 August 2018

Why we declare WebDriver driver = new FirefoxDriver(); and not FirefoxDriver driver = new FirefoxDriver();

Why do we declare WebDriver driver = new FirefoxDriver();
and not FirefoxDriver driver = new FirefoxDriver();
To put in other words, why List<String> list = new ArrayList<String>();
and not ArrayList<String> list = new ArrayList<String>();


Here, List or WebDriver is an Interface - a contract or set of rules created for implementing class.
So, in Java, we cannot create an instance of interface. ie
I cannot say List<String> list = new List<String>();
But classes can be instantiated. Hence,
FirefoxDriver fd= new FirefoxDriver(); and
ArrayList list=new ArrayList<String>();
are meaningful. Because both are classes in Java.
Now, coming back to the same question. When we say FirefoxDriver fd= new FirefoxDriver(); or ArrayList list=new ArrayList<String>();
This is nothing but using specific implementation. Arraylist or FirefoxDriver. If we have coded like this, then in the future, if we want to switch to
other implementations of List or WebDriver... then we need to modify our complete code as there is no guarantee that the rest of the code doesn't make use of methods specific to the particular class. However, if given like this:
List<String> list = new ArrayList<String>();
WebDriver driver = new FirefoxDriver();
It gives the flexibility to switch implementation any time with minimal rework.
For eg: changing ArrayList to LinkedList you can change the code to List<String> list = new LinkedList<String>(); from List<String> list = new ArrayList<String>();.

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule

How to schedule RFT (Rational Functional Tester) scripts to run using Jenkins / schedule 1. Create a batch file with following content ...