About Me

Page views

Powered by Blogger.

Followers

Wednesday 1 July 2015


Common Exceptions while automating :
NoSuchElement : An element could not be located on the page using the given search parameters.
NoSuchFrame : A request to switch to a frame could not be satisfied because the frame could not be found.
StaleElementReference : An element command failed because the referenced element is no longer attached to the DOM.
Firefox Not Connected : Firefox browser upgraded toop new version.
ElementIsNotSelectable : An attempt was made to select an element that cannot be selected.
UnknownCommand : The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.
ElementNotVisible : An element command could not be completed because the element is not visible on the page.
InvalidElementState : An element command could not be completed because the element is in an invalid state (e.g. attempting to click a disabled element).
UnknownError : An unknown server-side error occurred while processing the command.
JavaScriptError : An error occurred while executing JavaScript code.
XPathLookupError : An error occurred while searching for an element by XPath.
Timeout : An operation did not complete before its timeout expired.
NoSuchWindow : A request to switch to a different window could not be satisfied because the window could not be found.
InvalidCookieDomain : An illegal attempt was made to set a cookie under a different domain than the current page.
UnableToSetCookie : A request to set a cookie’s value could not be satisfied.
UnexpectedAlertOpen : A modal dialog was open, blocking this operation
NoAlertOpenError : An attempt was made to operate on a modal dialog when one was not open.
ScriptTimeout : A script did not complete before its timeout expired.
InvalidElementCoordinates : The coordinates provided to an interactions operation are invalid.
IMENotAvailable : IME was not available.
IMEEngineActivationFailed : An IME engine could not be started.
InvalidSelector : Argument was an invalid selector (e.g. XPath/CSS).

Friday 26 June 2015


How to launch chrome browser using selenium webdriver 

package DayFour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import com.thoughtworks.selenium.Selenium;
public class Chrome_Test {
    public WebDriver driver;
    public Selenium selenium;
   
    @Test
        public void LaunchChromeBrowser()
        {
        driver.get("http://manualtestinglearn.blogspot.in/");
        }
   
    @BeforeTest
        public void beforeTest()
        {
         //---> for Windows machine
        System.setProperty("webdriver.chrome.driver", "D:\\Library\\chromedriver.exe");  // Your chromedriver path.
        driver=new ChromeDriver();
       
        //---> for Linux machine
        System.setProperty("webdriver.chrome.driver",  "/home/Seleniumyourself/lib/chromedriver");   // Your chromedriver path.
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        }
    @AfterTest
        public void afterTest() {
    }
}

Thursday 25 June 2015



So How to click on web link, image and tab on webpage using Selenium WebDriver.

Steps :
·         Define Firefox Browser and open the Firefox Browser
·         Open the URL (Website)
·         Get on IMAGE LINK like button using By.xpath
·         Get on WEBLINK using By.linktext  
·         Get on TAB LINK using By.cssSelector
·         Verify Correct Webpage is opened using click on TAB LINK and print on console
·         Verify Correct Webpage is not opened using click on TAB LINK and print on console.

Source Code : 

Image_Link_using_XPATH -           driver.findElement(By.xpath(".//*@id='choice']/tbody/tr/td[2]/center/a[1]/img")).click();
WebLink using Linktext -                   
     driver.findElement(By.linkText("the documentation")).click();
TAB Link using cssSelector -                                     
     driver.findElement(By.cssSelector("#header ul a")).click();

Practice Yourself :


package WebDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.junit.Assert.assertTrue;

public class Click_on_Link_Image_Tab {
          public static void main(String[] args) throws InterruptedException {
                  
                   //Define Firefox Browser as webdriver and open the Firefox Browser
                   WebDriver driver = new FirefoxDriver();
                  
                   //Open the website (URL)
                   driver.get("http://seleniumhq.org");
                  
                   //Click on IMAGE LINK using cssselector
             driver.findElement(By.xpath(".//*@id='choice']/tbody/tr/td[2]/center/a[1]/img")).click();
                  
                   //Click on WEBLINK using linkText
                   driver.findElement(By.linkText("the documentation")).click();
                  
                   //Click on TAB LINK using ID
                   driver.findElement(By.cssSelector("#header ul a")).click();
                  
                   try {
                             // Verify Correct Webpage is opened using click on TAB LINK
                             assertTrue(driver.getPageSource().contains("About Selenium"));
                             //Message is printed when webpage is opened using click on TAB LINK
                             System.out.println("Webpages are correctly opened");
                   }
                   catch (Throwable e)
                   {
                             //Message is printed when webpage is opened using click on TAB LINK
                             System.out.println("Webpages are NOT correctly opened");
                   }
                  
                   // Close the Firefox Browser
                   driver.close();
                  
          }

}

Sunday 21 June 2015



How to count the number of items in the drop down field using size() using Selenium WebDriver.



Steps:

      1.  Define Firefox Browser and open the Firefox Browser
       2.  Open the URL (Website)
       3.  Assign and Select the drop-down list element
    4.  Get all the option from drop-down list and assign into List
      5.  Count the item drop-down list and assign into integer variable


package dayOne;


import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Count_Item_Dropdown {
       public static void main(String[] args) {
             
              //Define the Webdriver for Browser i.e. Firefox
              WebDriver driver = new FirefoxDriver();
               
              //Open the URL (Website)
        driver.get("http://housejoy.in/");
       
        //Assign and Select the dropdown list element
        Select selectDropdown = new Select(driver.findElement(By.id("cityName")));
       
        //Get all the option from dropdown list and assign into List
        List<WebElement> listOptionDropdown = selectDropdown.getOptions();
       
        // Count the item dropdown list and assign into integer variable
        int dropdownCount = listOptionDropdown.size();
       
        //Print the total count of dropdown list using integer variable
        System.out.println("Total Number of item count in dropdown list = "  + dropdownCount);      
       
       }
}


Friday 22 May 2015


So How to get any text is available in HTML Page Source Code using Selenium WebDriver.


Steps :
  1.  Define Firefox Browser and open the Firefox Browser
  2.  Open the URL (Website)
  3.  Identify the number of Links on webpage and assign into Webelement List
  4. Count the total Link list on Web Page 
  5. Print the total count of links on webpage
  6. Identify all the elements on web page
  7. Count the total all element on web page
  8. Print the total count of all element on webpage
  9. Print all the Tag Name and Text Name on webpage.

Practice Yourself :

package dayOne;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Count_Total_Weblink_and_AllElement_on_Webpage {

       public static void main(String[] args) {
             
              //Define the Webdriver for Browser i.e. Firefox
              WebDriver driver = new FirefoxDriver();
               
              //Open the URL (Website)
        driver.get("http://yahoo.com");
       
        //Identify the number of Link on webpage and assign into Webelement List 
        List<WebElement> allLinkElements = driver.findElements(By.xpath("//a"));
       
        // Count the total Link list on Web Page 
        int linkListCount = allLinkElements.size();
               
        //Print the total count of links on webpage
        System.out.println("Total Number of link count on webpage = "  + linkListCount);    
             
       //Identify all the elements on web page
       List<WebElement> allElements = driver.findElements(By.xpath("//*"));
      
       //Count the total all element on web page
       linkListCount = allElements.size();
    
       //Print the total count of all element on webpage
       System.out.println("Total Number of All Element on webpage = "  + linkListCount);   
      
       //Print all the Tag Name and Text Name on webpage
       int i = 0;
        for (WebElement Element : allElements) {
            i = i +1;
            System.out.println(Element.getTagName());
            System.out.println(Element.getText());
        }
        
        //Close the Broswer
       driver.close();
      
       // Quit the selenium
       driver.quit();
       
       }
}

Sunday 17 May 2015

So how to handle javascript alerts, confirmation and prompts?


JavaScript popups are generated by web application and hence they can be easily controlled by the browser.
Webdriver offers the ability to cope with javascript alerts using Alerts API
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
Alert is an interface. There below are the methods that are used
//Will Click on OK button.
alert.accept();
// Will click on Cancel button.
alert.dismiss()
//will get the text which is present on th Alert.
alert.getText();
//Will pass the text to the prompt popup
alert.sendkeys();
//Is used to Authenticate by passing the credentials
alert.authenticateUsing(Credentials credentials)

This program will demonstrate you working on Alerts popup using above html file.

Saturday 9 May 2015



Creation of Sample TestNG project
Let us begin with the creation of TestNG project in eclipse IDE.
Step 1: Click on the File option within the menu -> Click on New -> Select Java Project.
Selenium TestNG tutorial 8
Step 2: Enter the project name as “DemoTestNG” and click on “Next” button. As a concluding step, click on the “Finish” button and your Java project is ready.
Selenium TestNG tutorial 9
Step 3: The next step is to configure the TestNG library into the newly created Java project. For the same, Click on the “Libraries” tab under Configure Build Path. Click on “Add library” as shown below.
Selenium TestNG tutorial 10
Step 4: The user would be subjected with a dialog box promoting him/her to select the library to be configured. Select TestNG and click on the “Next” button as shown below in the image. In the end, click on the “Finish” button.
Selenium TestNG tutorial 11
The TestNG is now added to the Java project and the required libraries can be seen in the package explorer upon expanding the project.
Selenium TestNG tutorial 12
Add all the downloaded Selenium libraries and jars in the project’s build path as illustrated in the previous tutorial.

Creating TestNG class

Now that we have done all the basic setup to get started with the test script creation using TestNG. Let’s create a sample script using TestNG.
Step 1: Expand the “DemoTestNG” project and traverse to “src” folder. Right click on the “src”package and navigate to New -> Other..
Selenium TestNG tutorial 13
Step 2: Expand TestNG option and select “TestNG” class option and click on the “Next” button.
Selenium TestNG tutorial 14
Step 3: Furnish the required details as following. Specify the Source folder, package name and the TestNG class name and click on the Finish button. As it is evident from the below picture, user can also check various TestNG notations that would be reflected in the test class schema. TestNG annotations would be discussed later in this session.
Selenium TestNG tutorial 15
The above mentioned TestNG class would be created with the default schema.
Selenium TestNG tutorial 16
Now that we have created the basic foundation for the TestNG test script, let us now inject the actual test code. We are using the same code we used in the previous session.
Scenario:
  • Launch the browser and open “gmail.com”.
  • Verify the title of the page and print the verification result.
  • Enter the username and Password.
  • Click on the Sign in button.
  • Close the web browser.
Code:
1package TestNG;
2import org.openqa.selenium.By;
3import org.openqa.selenium.WebDriver;
4import org.openqa.selenium.WebElement;
5import org.openqa.selenium.firefox.FirefoxDriver;
6import org.testng.Assert;
7import org.testng.annotations.Test;
8
9public class DemoTestNG {
10       public WebDriver driver = new FirefoxDriver();
11       String appUrl = &quot;https://accounts.google.com&quot;;
12
13@Test
14public void gmailLogin() {
15             // launch the firefox browser and open the application url
16              driver.get(&quot;https://gmail.com&quot;);
17              
18// maximize the browser window
19              driver.manage().window().maximize();
20              
21// declare and initialize the variable to store the expected title of the webpage.
22              String expectedTitle = &quot; Sign in - Google Accounts &quot;;
23              
24// fetch the title of the web page and save it into a string variable
25              String actualTitle = driver.getTitle();
26              Assert.assertEquals(expectedTitle,actualTitle);
27              
28// enter a valid username in the email textbox
29              WebElement username = driver.findElement(By.id(&quot;Email&quot;));
30              username.clear();
31              username.sendKeys(&quot;TestSelenium&quot;);
32
33// enter a valid password in the password textbox
34              WebElement password = driver.findElement(By.id(&quot;Passwd&quot;));
35              password.clear();
36              password.sendKeys(&quot;password123&quot;);
37              
38// click on the Sign in button
39              WebElement SignInButton = driver.findElement(By.id(&quot;signIn&quot;));
40              SignInButton.click();
41              
42// close the web browser
43              driver.close();
44}
45}
Code Explanation with respect to TestNG
1) @Test – @Test is one of the TestNG annotations. This annotation lets the program execution to know that method annotated as @Test is a test method. To be able to use different TestNG annotations, we need to import the package “import org.testng.annotations.*”.
2) There is no need of main() method while creating test scripts using TestNG. The program execution is done on the basis of annotations.
3) In a statement, we used Assert class while comparing expected and the actual value. Assert class is used to perform various verifications. To be able to use different assertions, we are required to import “import org.testng.Assert”.

Executing the TestNG script

The TestNG test script can be executed in the following way:
=> Right click anywhere inside the class within the editor or the java class within the package explorer, select “Run As” option and click on the “TestNG Test”.
Selenium TestNG tutorial 17
TestNG result is displayed into two windows:
  • Console Window
  • TestNG Result Window
Refer the below screencasts for the result windows:
Selenium TestNG tutorial 18
(Click on image to view enlarged)
Selenium TestNG tutorial 19

HTML Reports

TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browser and it can also be viewed using Eclipse’s build –in browser support.

To generate the HTML report, follow the below steps:

Step 1: Execute the newly created TestNG class. Refresh the project containing the TestNG class by right clicking on it and selecting “Refresh” option.
Step 2: A folder named as “test-output” shall be generated in the project at the “src” folder level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.
Selenium TestNG tutorial 20
Selenium TestNG tutorial 21
Step 3: The HTML report shall be opened with in the eclipse environment. Refer the below image for the same.
Selenium TestNG tutorial 22
Refresh the page to see the results for fresh executions if any.

Setting Priority in TestNG

Code Snippet
1package TestNG;
2import org.testng.annotations.*;
3public class SettingPriority {
4
5@Test(priority=0)
6public void method1() {
7 }
8
9@Test(priority=1)
10public void method2() {
11 }
12
13@Test(priority=2)
14public void method3() {
15 }
16}

Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com