Wednesday, October 29, 2014

Example of firefoxDriver Class

Using selenium to simulate browser we have classes for different browser for example we have FirefoxDriver class for firefox browser. So any simulations can be performed on browser by methods of the class  FireforDriver and there are many methods written under it for instance close and open of the browser.
WebDriver is API it is available as "JAR" file or we can say its a zip file in JAVA. User can write their own script and making JAR of the same.

Lets discuss scenario of opening and closing of browser using WebDriver:-

FirefoxDriver objvar = new FirefoxDriver();
objvar.close();

this code will open/launch firefox browser and in next line will close the browser.

By writing new FirefoxDriver();

it will create a object of FirefoxDriver and to use we need to store it in a variable which needs to be of FireforDriver type.

i.e FirefoxDriver objvar = new FirefoxDriver();

Objvar is variable in which reference of the object of firefoxDriver class is stored .

Now when we write objvar. we will get list of all static and non-static methods and other variables available under FirefoxDriver class

At the time of execution it will open the firefox browser and then close it , just because at the time of object creation it calls the constructor of the class which has the code to launch the Firefox browser.

Other  basic method available are Click(), getText(), getAttribute("") and so on .

Lets take example in which we can validate that we have logged in the page successfully.
After login page says "Welcome username".

So we can fetch the text welcome and check if we get "welcome " we can say test case pass else fail

Example:-

String text =objvar.findelementByXPath("//xpathofwelocome").getText();
and then we can compare it to get results .

If we want to fetch that value from any attribute in that case we will write this
String attri = bjvar.findelementByXPath("//xpathofwelcome").getAttribute("attributename");  


No comments:

Post a Comment