Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Sunday, 30 November 2014

How to Capture screenshots in Selenium WebDriver

Capturing screenshots in selenium Webdriver

When we run our Selenium test on server sometime our test case failed.Debugging of each and every test cases is very challenging.If we take screenshots of everytest case it helps us to complete our task.

 And sometime we have a requirement that you have to take screenshot of every Test case.Selenium provides an interface which helps us to take screenshots of Everytestcase.

I am writing that two lines of code which helps you to take screenshot in Selenium Webdriver

 // take the screenshot at the end of every test
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  //WebDriver provides an interface TakesScreenshots for capturing the screen shots of every testcase/failed Testcase.
//getScreenshotAs is a method to capture screenshots and saves into temporary location 
        FileUtils.copyFile(scrFile, new File("c:\\screenshot.png"));
//FileUtils.copyFile() is used to copy this temporary file to the wokspace.

Here is the code of taking sceenshots of failed testcase
package test;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Chrome {
WebDriver driver;
@BeforeTest
public void openBrowser()
{
driver = new FirefoxDriver();
driver.get("http://facebook.com");
}
@Test
public void getScreenshot()
{
try {
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new
File("e:\\facebook_page.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void closeBrowser()
{
driver.close();
}
}

//You need to append the path Eveytime

Thursday, 27 November 2014

What is xpath

  • In automation we need to perform some operations on Webelement.
  • For any website application there are some common pages on which you found button,textbox,radiobutton,dropdown button etc.
  • For performing the action the first thing you need to find the xpath for it. 
                                                            What is xpath?
                                                        
In every web-based applications the XPaths or locators are unique addresses for each and every web object. It can be used with selenium to perform operations on each object present in the webpage. In the web page each web-object has a primary unique XPath.

Xpath detailed explations.The use of xpath.

  • Selenium actully uses xpath to identify elements.
  • Webpage has varies of object(eg:images,textboxes,textfields,buttons,radiobutton,checkboxes etc.)
  • Each and every object has a unique id/name/path called xpath.
  • If developer change the color of button in a webpage later but xpath remain same.
  • If you are willing to become an automation tester in IT industry,Learning xpath is makes the thing easy.
  • When automate a website,have to identify the object of the page and every webpage according to testcase.

How to Find xpath

How we get xpath for Mozilla Firefox
If  your webpage is running of firefox browser it is very easy to get xpath very easily.You just need to install two addons on Mozilla(Firebug and FirePath)
1.Download Firebug
 But before inspecting  you need to download Firepath tool.
2.Download Firepath
After installing FirePath you can start finding xpath of any webelement.

There are two type of xpath.
1.Absolute xpath
2.Relative xpath
Absolute path will start with root path (/) and Relative path will from current path (//) 
Absolute xpath
Let me take an example of https://www.facebook.com/
Go to https://www.facebook.com/
Now press F12 or fn+F12
                                                     Tool inspector for Finding xpath


Absolute xpath:

If you need to find out Absolute xpath select this General absolute xpath.It will give you absolute xpth.


Absolute xpath of email Textbox.

 Relative xpath:

If you need to find out relative xpath then uncheck the Generate absolute xpath.


Absolute xpath of email textbox.
Difference Between Absolute and Relative xpath:

Absolute xpath sometime fails because sometime developer change the div.So if you will use absolute xpath technically it work but if any node change then xpath will not work.
So you need to find out Relative xpath.

Differences in Absolute and Relative xpath

Absolute XPath starts with the root node or a forward slash (/).
Advantage
 It identifies the element very fast.
Disadvantage
 If any other tag added in between, then this path will no longer works.
Suppose Absolute xpath is
html/body/div[1]/div[1]/div/div/div/div/div/div/form/table/tbody/tr[2]/td[1]/input

If any of the div add in this xpath then this xpath will fail.
html/body/div[1]/div[1]/div/div/div/div/div2/div/div/form/table/tbody/tr[2]/td[1]/input
Relative xpath
1 Starts with //
2.//input” matches all the paragraph elements starts with input
3. //input[@id='email'] is a relative xpath.
Some basic concept you should all know.
Go to this link

Click on inspector and take the curser to your username.This will give you an xpath.

Note:This firebug always give you an xpath which is xpath by id.

But in selenium sometimes id may change so you have to take a different attributes.
How to write xpath mannually.

Syntax for path
//*[@attribute='value']
//is used to find relative element
* is used for tagname..Tagname is always input for textbox,radiobutton,button.

Tagname is always available for any element.
Tagname is always written with this bracket Example <input,<p<li
Input,p,li are the tag name.
xpath for username
<input id="username" type="text" maxlength="40" value="" name="username">
input is tag name
Attributes are id,type,maxlength,name
values are username,text,40,username.Values are written in orange color.
//*[@attribute='value']

You can write xpath as:
Your first prefernce should be find id of that element
//input[@id='username']

After that you can use any attribute
Xpath by name
//input[@name='username']
Sometime attributes are same so you all need to find out unique element for object.

What is FirePath

What is FirePath?

FirePath is a Firebug extension that adds a development tool to edit, inspect and generate XPath
1.0 expressions, CSS 3 selectors and JQuery selectors (Sizzle selector engine)
How to Install FirePath

Step1: Download Firepath
Important:
FirePath
is an extension of Firebug. So, please make sure to install Firebug to your Firefox
web browser before to install FirePath. And make sure to surf this using Firefox web browser.



Step2:Click on Add to Firefox

Step3:Click on Allow


Step4:Download starting

Step5: Click on Install

Step6:FirePath has been installed.But you need to Restart your firefox.

What is Firebug

What is Firebug?
Firebug is a web development tool that facilitates the debugging, editing, and monitoring of any
website's CSS, HTML, DOM, XHR, and JavaScript; it also provides other web development tools.
It simply install addon on firefox.

How to install firebug.

Step1:  Download Firebug
Step2:Click on add to firefox.

Step3:Click on Allow.

Step4:After clicking on allow.Downloading will Start.

Step5:Now click on Install

Step6:Firebug is Installed.

Now you can inspect element of the webpage using Firebug.

Wednesday, 26 November 2014

How to Upload File using autoit.

Before Using AutoIt first you have to know few things about autoit.

Three simple steps we have to follow always When we are using auto it.
ControlFocus-This will give focus on the window
ControlSetText-This will set the file path
ControlClick-This will click on button
1. Select the window and get the focus on it.

2. Type the file name with path in the text box.

3. Click open button, so that all the selection details are transferred into web page control and
selenium can take control of the next activity.


Scenario for uploading file using Auto it.
1.Go to 
http://api.checklist.com/login
2.Enter Email as rgulati883@gmail.com
3.Enter Password as jaiguruji
4.Click on Login
5.Click on Checklist1
6.Click on upload
7.Then add path of your File
8.Click on open button.
9.File Added


1.Enter email= rgulati883@gmail.com
2.Enter password = jaiguruji
3.Click on Login
                         
4.Click on Checklist1(0)
                              


5.Go to that path Where your AutoIt is Downloaded>Click on AutoIt

6.Click on AutoIt.You will get this Finder Tool
7.Click on Upload
8.Browser window will open
9.Drag this FinderTool to Filename Textbox

10.Note Title of Window = Open
11.Class = Edit
12.Instance = 1
ControlID = Class+Instance
           

13.Click on ScITE and Write a Smal Script into that




14.Write a small script into it.Make sure in  Google chrome Browser Title of window is (open) 
 Title of Window in Mozilla Browser  is (File Upload).


15. ControFocus("TitleofWindow","","Class+Instance")
       ControFocus("Open","","Edit1")
16.ControlSetText("TitleofWindow","","Class+Instance","Path of Your File")
     ControlSetText("Open","","Edit1","Path of Your File")      
17.After adding path we Have to Click Open Button for Uploading File
18. Drag the Finder tool to the Open Button


19. ControlClick("Title of window","","ControId")
 ControlClick("Open","","Button1")
20. Save the script to a location.Script will be save as .au3 extension.After saving you will get this                          

             

21.After saving it compile this script. Rightclick>Compilescript


22. After compiling you will get this 

23. Now copy the path of this file.  ("C:\\Users\\Hp\\Desktop\\tesing.exe").
24. Now write Selenium program and add this .exe file and run your program.

package test;

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestFileUpload2 {

public static void main(String[] args) throws InterruptedException, IOException {

WebDriver driver = new FirefoxDriver();
driver.get("http://api.checklist.com/login");
driver.findElement(By.xpath("//*[@id='email']")).sendKeys("rgulati883@gmail.com");
driver.findElement(By.xpath("//*[@id='loginForm']/div[2]/input")).sendKeys("jaiguruji");
driver.findElement(By.xpath("//button[@class='btn btn btn-primary pull-right']")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[@id='userChecklists']/li[1]/a/span")).click();
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='taskUploadFile']"));
 UploadImg.click();
 Runtime.getRuntime().exec("C:\\Users\\Hp\\Desktop\\tesing.exe");
 Thread.sleep(2000);   
}
}

                            

Download AutoIt

Whats the Need of AutoIt in Selenium

Selenium core JavaScript engine don't have capability to handle the windows pop-up due to same origin policy, so we need to  use some other tools.
  It will be handled through AutoIT.
Once you are able to click on browse button and a dialog box is open to choose the file then you just run a AutoIT script which will help to select the file from your local or remote drive and control will come to your web page to proceed with selenium.

What is Auto It.

1-Autoit is an open source tool which can work with Desktop Applications.
2-It uses a combination of simulated keystrokes,mouse movement and window/control manipulation in order to automate tasks in a way not possible with other languages (eg:VBScript and Send Keys).

Download Autoit

Step1:Click on Download Autoit

Step2:After clicking on Download you will get a Auto it setup..


Step3:Double click on this set up and install it on to Your system.
Step4:Click on Run
Step5:Click on Next

Step6:Click on I agree
Step7:Wait for sometime until installation is not finished.
Step8:Autoit is installed on your system.

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: