Wednesday, 8 February 2017

Selenium - Creating Xpath using text of the object

As all knows xpath is differentiated in 2 ways while getting used in selenium, one is absolute and other is relative. Nowadays chrome browser helps user with the option available in the browser to copy the xpath which can be used directly in the script.

But many times the xpath provided by the browser is not correct or the property used in that is dynamic. So we have to write it down. I am providing the below examples which will help you to write down the xpath with correct syntax's and different identifiers also.

Using Text of the Object

If you don't know the html tag of the object.

"//*[text()='text of object']"

If you know the html tag of the object.

"//a[text()='text of object']"

If don't know the whole text but only few character. But make sure it should be unique.

"//*[contains(text(),' character of the text object')]"

"//*[contains(@id,' character of the id')]"



Selenium - How to handle login popups in Chrome browser

I was facing issues while dealing with one of the applications where the login popup is coming to fill the user id and password which is not the web elements to be handled using the selenium webdriver.

I have come across a solution which is very simple to be used in chrome. We just need to append the userid and password with the url of the application and load it into the browser.

 String[] URLText = URL.split("//");
 String URL2 = "//" + UserID + ":" + Password + "@" + URLText[1];
 URL = URLText[0] + URL2;
 driver.get(URL);

I hope it will resolve the issues if any one faces same.

I will share the same for Internet explorer in my next post.

Tuesday, 9 August 2016

How to switch window using Selenium


Selenium provides the function windowhandle for the driver. this functions can be used to find out all the available windows in a driver and then can be switched if it is different from the previously opened window.

Below is the logic which i have used:

String a = driver.getWindowHandle();

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

if(b.size() > 1){
for(String windowHandle  : b)
       {
if(windowHandle.equals(a))
{
       //Do Nothing
}
else
{
driver.switchTo().window(windowHandle);
}

       }

Monday, 29 February 2016

How to create and update a word document in Java

Below code snippets will help you to create a new word document and update an existing word document. We are using apache poi libraries to access word documents.

Below code will create a word document of name FileName.
                 //Blank Document
                   XWPFDocument document= new XWPFDocument();
                //Write the Document in file system
                   FileOutputStream out = new FileOutputStream(new File(FileName));
                   document.write(out);
                   out.close();

Below code will update a word document of name FileName and will insert a local image.

                   XWPFDocument document = new XWPFDocument(new FileInputStream(FileName));
  XWPFParagraph a = document.createParagraph();
  XWPFRun run=a.createRun();
  run.setText(ImageText);
 
  String imgFile = ImagePath;
  FileInputStream is = new FileInputStream(imgFile);
  run.addBreak();
  run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile,                                                  Units.toEMU(500), Units.toEMU(300));
  FileOutputStream out = new FileOutputStream(new File(FileName));
  document.write(out);
  out.close();


Please let me know if you face any issues while using the above code.

Tuesday, 19 January 2016

How to read Excel file using java for Selenium

To read an excel file using java we need to download 4 jar files which will help to read excel files. So whenever you are starting to read the file below is the list of jar files to be used:

1. dom4j<latest ver>.jar
2. poi<latest ver>.jar
3. poi ooxml <latest ver>.jar
4. poi ooxml shcemas <latest ver>.jar

As soon as you configure these jar files in your build path, below code can be used which can extract data from excel files:

        FileInputStream ExcelFile = new FileInputStream("<File Name>.xlsx");
        XSSFWorkbook ExcelWBook = new XSSFWorkbook(ExcelFile);
  XSSFSheet ExcelWSheet = ExcelWBook.getSheet(SheetName);
      XSSFCell Cell = ExcelWSheet.getRow(rownum).getCell(column);
        String Celldata = Cell.getStringCellValue();

XSSF is used for .xlsx files and HSSF is used for .xls files.

Monday, 21 December 2015

Scrolling to right using Java in Selenium tests

Many times testers face issues related to scrolling to right of the application so that object is visible.
Below is the code which i have used and working perfectly fine.

You can use action or scrolling horizontally using the right or left key on the keyboard. The library which needs to be included is "org.openqa.selenium.interactions.Actions".


Actions action = new Actions(driver);
action.sendKeys(Keys.ARROW_RIGHT).perform();


For doing the scrolling vertically, you can use the below code:

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].scrollIntoView();", <webelement>);

This has not worked for me when scrolling horizontally that is the reason i have above solution different for horizontal and vertical scrolling.

Wednesday, 9 September 2015

Creation of New Project in Eclipse and Addition of Selenium Jar files and TestNG Library

1    Note: Please download Selenium jar files and TestNG library before starting below steps.

             1.  Create a new Java Project in eclipse

2.      Click on Next and add Selenium Jar files (selenium-server-standalone-2.47.1, selenium-java-2.47.1 and selenium-java-2.47.1-srcs) and TestNG files. For TestNG files click on “Add Library” and to add Selenium Jar files click on “Add External Jars”







 3.      Expand the project in Java Eclipse and check the added files: