Wednesday, September 12, 2018

How to Identify and Handle Different Pop-ups in Selenium

Hello Friends ,

In this Post , I am  going to show how to identify different kind of Popups and how to handle them in Selenium.


1- Hidden division Popup


Tips to Identify:


  • We can inspect this Popup with right click-Inspect
  • This Popup we cannot move 
  • Also this Popup will be colorfull.

By using the above tips you can easily identify Hidden Division Pop-Up



Handling Technique:


Since We can inspect these kind of Popup , so we  can easily find elements in this Pop-up using

driver.findElement(By.xpath("xpath of element"));



2- JavaScript Popup :


Tips to Identify:



  • Javascript Popup appears just below (for chrome browser) the address bar  at center place
  • For Browsers other than chrome , it appears in middle of the page
  • Javascript Popup have just  OK button for Alert Popup 
  • For Confirmation Alert it has OK and Cancel button
  • We cannot inspect Element in this Popup 






Handling Technique:


We can handle this alert Popup using  switchTo().alert() method

a- Get the Text of alert Popup -

driver.switchTo().alert().getText();

b-Click OK-

driver.switchTo().alert().accept();

c-Click Cancel in Confirmation Popup -

driver.switchTo().alert().dismiss();



3- Child Browser Popup :


Tips to Identify:



  • We can inspect this Popup  with right click-Inspect
  • We can move this Popup 
  • Popup will be colourfull
  • Will have minimize and maximize button




Handling Technique:


Whenever we open any browser , one window handle id is allocated to that browser.

So All  opened browser (parent and child ) are having their respective window handle ids

To get the window handle id of current  browser we use method 


driver.switchTo().window(window handle );


Example- Switching to each child browsers
      
                        //Enter URL in browser

                driver.get("https://www.naukri.com/");

      //get parent window handle

String parentwindow = driver.getWindowHandle();

                      //get all opened browsers window handle ids and store in a list

Set<String> allWindows = driver.getWindowHandles();

                      //Print the count of all opened windows

System.out.println(allWindows.size());

     //remove parent window id from All windows handle ids

allWindows.remove(parentwindow);
                     //using for each loop, iterating to each window

for (String s : allWindows) {
 
System.out.println(s);

                    //switching to child windows and perform action

driver.switchTo().window(s);

System.out.println(driver.getTitle());
}
                    //switch back to parent window

driver.switchTo().window(parentwindow);



4- File Upload Popup :


Tips to Identify:



  • When we click on Browse,Upload, Attach icon , this Popup appears
  • We can move this Popup but we cannot inspect it
  • This Popup is used to Select the document/file from Local machine.






Handling Technique:


In this technique , we do not click on upload button, 

To Handle this Popup   below are the steps followed:

1-Inspect and find Upload button
2-Use sendKeys() method and give absolute path of the file stored in local PC as and argument to sendKeys() method.

driver.findElement(By.xpath("//input[@value='Upload CV']")).sendKeys("D://sample resume.doc");




5- Notification Popup :


Tips to Identify:


  • This Popup appears just below the address bar but at the left side of the page.
  • We cannot move this Popup 
  • This Popup having Allow and Block button






Handling Techniques ( for chrome browser):


We need to change settings of chrome browser,  this can be done using ChromeOptions class.
for other browser need to use repective browser Options class to change the settings

             ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notification");
WebDriver driver = new ChromeDriver(options);







Monday, September 3, 2018

MySQL DataBase Queries for Selenium Automation Testing Interviews

Hi Guys ,

While working as an Automation Test Engineer  or in any Automation Testing Interviews,we have to deal  with database most of the time.
In this post I am sharing frequently asked MySQl Queries which will be very  usefull in Interview.


1-Create MySQL DataBase

create database Devtest;


2-Select database:

We need to select Database before using it.

use Devtest;


3-Create Table:

Table 1: Employees:

create table Employees(Employee_id  varchar(10) not null, First_name varchar(20),Last_name varchar(20), Salary int(10), Joining_date datetime , Department varchar(20) );

Table 2: incentives:

create table incentives(Employee_id  varchar(10) not null, First_name varchar(20),Last_name varchar(20), Salary int(10), Joining_date datetime , Department varchar(20) );


4-Insert Record to Tables:

Insert Record to Table1:

Insert into Employees (Employee_id,First_name,Last_name,Salary,Joining_Date,Department) 
Values(1,"John","Abraham",100000,"13-01-01 12:00:00","Banking"),
(2,"Michael","clarke",80000,"13-01-01 12:00:00","Insurance"),
(3,"Virat","Kohli",70000,"13-02-01 12:00:00","Banking"),
(4,"Anil","Kumble",60000,"13-02-01 12:00:00","Insurance"),
(5,"Bryan","Lara",65000,"13-02-01 12:00:00","Insurance"),
(6,"Amit","Kumar",75000,"13-01-01 12:00:00","Services");

Insert Record to Table2:

Insert into Incentive (Employee_ref_id,Incentive_date,Incentive_amount) 
Values(1,"13-02-01 12:00:00",5000),
(2,"13-02-01 12:00:00",3000),
(1,"13-01-01 12:00:00",4500),
(2,"13-01-01 12:00:00",3500),
(3,"13-02-01 12:00:00",4000);


5- Get All Records from Table

Table 1: Employees

select * from Employees ;



Table 2: Incentive

select * from Incentive ;



6-Get first name ,last name from Employees table

Select First_name,Last_name from Employees;



7-Get First_Name from Employees table in upper case

Select upper(First_name) from Employees;



8-Get First_Name from Employees table in lower case

Select lower(First_name) from employee;



9-Get unique DEPARTMENT from employee table

Select distinct department from Employees;


10- Get FIRST_NAME from Employees table after removing white spaces from right side

select rtrim(First_name) from Employees;



11-Get FIRST_NAME from Employees table after removing white spaces from Left side

select ltrim(First_name) from Employees;



12-Get first 3 characters of FIRST_NAME from Employees

select substring(First_name,1,3) from Employees;




13-Get length of FIRST_NAME from Employees table

select length(First_name) from Employees;




14-Get First_Name from Employees table after replacing 'o' with '$'

select replace(First_name,'o','$') from Employees;




15-Get First_Name and Last_Name as single column from Employees table separated by space.


select concat(first_name," ",last_name) as Full_name from Employees ;





16-Get FIRST_NAME ,Joining year , Joining Month and Joining Date from Employees table

select First_name,year(Joining_date),month(joining_date),day(joining_date) from Employees ;





17-Get all employee details from the Employees table order by First_Name Ascending

select first_name from Employees  order by First_name asc;





18-Get all employee details from the Employees table order by First_Name Descending

select first_name from Employees  order by first_name desc;





19-Get Employees details from employee table whose Employees name are “John” and “Roy”

select * from Employees where first_name not in ("john","roy");





20-Get employee details from Employees table whose first name starts with 'b'

select * from Employees  where First_name like "b%";





21-Get employee details from Employees table whose first name contains 'r'

select * from Employees where first_name like "%r%";





22-Get employee details from Employees table whose Salary greater than 70000

select * from Employees  where salary>70000;




23-Get employee details from Employees table whose Salary between 70000 and 90000

select * from Employees  where salary between 70000 and 90000;





24-Get employee details from Employees table whose name is 'John' and 'Michael

select * from Employees  where first_name in("john","michael");





25-Get employee details from Employees table whose joining year is “2013”

select * from Employees where year(Joining_date)="2013";





26-Get employee details from Employees table whose joining month is “January”

select * from Employees  where month(joining_date)="1";





27-Get difference between JOINING_DATE and INCENTIVE_DATE from Employees and incentives table

select first_name,JOINING_DATE - INCENTIVE_DATE  from Employees a inner join incentive b on a.employee_id=b.Employee_ref_id;





28-Get department,total salary with respect to a department from Employees table

select department ,sum(salary) as total_salary from Employees  group by department;









If you like my post , please don't forget to follow my blog.

Thanks for viewing this Post.






Wednesday, August 29, 2018

Common Selenium Exceptions





Common Selenium Exceptions  :


1-NoSuchSessionException :

When Webdriver perform action immediately after closing the browser, NoSuchSessionException Occurs.



2-StaleElementReferenceException:

When Element is no longer present on the DOM page. Example: The User has navigated to another page



  

3-NoSuchElementException:

When WebDriver is not able to find the element using findElement or findElements() method then NoSuchElementException occurs.



4-NoAlertPresentException:

When  WebDriver tries to switch to invalid alert , NoAlertPresentException Occurs

  

5-NoSuchWindowException:

When  WebDriver tries to switch to invalid window , NoSuchWindowException Occurs.


6-NoSuchFrameException:

When WebDriver tries to switch to invalid Frame , NoSuchFrameException Occurs.


7-WebDriverException:

Mostly when driver enter the URL  which does not contain http or https ,   driver.get(“url”) method throws WebDriver Exception.



8-TimeOutException:

When ExplicitWait condition does not meet within polling period,it gets TimeOutException.




Tuesday, August 28, 2018

XPath to CSS Selector Conversion

Hello Friends ,
In this post I am presenting how to convert Xpath to CSS Selectors.




HTML CODE:

<html>
   <body>
      <div>
      <a id="a1" class="c1" name="n1">Sample text</a> 
      </div>
      <div>Hello</div>
      </body>
</html>


1- select link having id="a1" using Absolute XPath


XPath -                                 /html/body/div/a[@id='a1']
CSS Selector-                      html>body>div>a[id='a1']


2- select link having id="a1" using Relative XPath 


XPath -                                 //a[@id='a1']
CSS Selector-                          a[id='a1']

XPath -                                 //html //a[@id='a1']
CSS Selector-                          html a[id='a1']



3- Select All links  in webpage:

XPath -                                 //a
CSS Selector-                      a

4-Select all first link in webpage:

XPath -                                 //a[1]
CSS Selector-                      a:nth-of-type(1)

5-Select first link out of all links using group index


XPath -                                ( //a)[1]
CSS Selector-                      css does not support group index

6- Select link by attribute ID

XPath -                                 //a[@id='a1']
CSS Selector-                      a#a1

7- Select link by attribute Class Name

XPath -                                 //a[@class='c1']
CSS Selector-                      a.c1

8- Select link by attribute Name

XPath -                                 //a[@name='n1']
CSS Selector-                      a[name="n1"]

9-Select link by using text()

XPath -                                 //a[text()='Sample text']
CSS Selector-                      css does not support text.


10-Select link by using AND Operation

XPath -                                 //a[@id="a1 and @name='n1']
CSS Selector-                      a[id="a1"][name='n1']

11-Select link by using OR Operation

XPath -                                 //a[@id="a1 or @name='n1']
CSS Selector-                      a[id="a1"],[name='n1']

12-Select link by using NOT (should select the link other than a1)

XPath -                                 //a[not(@id='a1')]
CSS Selector-                      a:not(id='a1')

13-Select link using contains

XPath -                                 //a[contains(@id,'a1')]
CSS Selector-                      a[id *= 'a1']

14-Select parent of link having id='a1'

XPath -                                 //a/parent::div
CSS Selector-                      div:has[>a]

15- Select parent of link having id='a1' using shortcut

XPath -                                 //a/..
CSS Selector-                      div:has[>a]


16- Select ancestor of link having id='a1'

XPath -                                 //a/ancestor::body
CSS Selector-                      body:has[ a]

17-Select following sibling of div Tag

XPath -                                 //div/following-sibling::div
CSS Selector-                      div + div





If You like my post ,please share this post and give your valuable feedback to improve my post.

Thanks.