Tuesday, December 30, 2014

Get Mouse Hover Text from Selenium WebDriver


In the article we are going to discuss  how to  retrieve TEXT from Mouse Hover on the Webelelment.

In this Program :

  • Navigating to https://www.facebook.com/
  • Mouse hover on facebook icon
       



  • Retrieve the text "Go to Facebook home" from below webelement'
            <a title="Go to Facebook Home" href="/">

  •  To Achieve this we need to identify the webelements using xpath.

 WebElement                      element1=driver.findElement(By.xpath(".//[@id='blueBarNAXAnchor']/div/div/div/div[1]/h1/
a"));

We were going to use Actions Class to perform mouse operation


Actions ac= new Actions(driver);
ac.moveToElement(element1).build().perform();


Source code :

import java.util.concurrent.TimeUnit;
 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.interactions.Actions;

public class mousehover{
       public static void main(String args[]) throws InterruptedException
       {
              WebDriver driver;
              driver = new FirefoxDriver();
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
           driver.get("http://www.facebook.com");
           Thread.sleep(7000);
           WebElement element1=driver.findElement(By.xpath(".//*[@id='blueBarNAXAnchor']/div/div/div/div[1]/h1/a"));
           Actions ac= new Actions(driver);
           ac.moveToElement(element1).build().perform();
           String hovertext=element1.getAttribute("title");
           System.out.println(hovertext);
       }

}

Run the above program

Output






Hope u Like this Post.
Add comments.








Sunday, December 28, 2014

TestNG Framework: Asserts and Reporters


So far we Seen Many of my previous examples programms in TestNG Framworks,

This Time we are going to take a look on Reports and Assert in TestNG.


TestNG Reporter Logs

TestNG also gives us the logging facility for the test. For example during the running of test case user wants some information to be logged in the console. Information could be any detail depends upon the purpose. Keeping this in mind that we are using Selenium for testing, we need the information which helps the User to understand the test steps or any failure during the test case execution. With the help of TestNG Logs it is possible to enable logging during the Selenium test case execution.
In selenium there are two types of logging. High level logging and Low level logging. In low level logging you try to produce logs for the every step you take or every action you make in your automation script. In high level logging you just try to capture main events of your test.
Everybody has their own style of logging and I have mine too. I am also a big fan of Log4j logging and that’s why I do not mix log4j logging with testng .
logging but on the same side I make to use of both of its. I perform low level logging with log4j and high level logging with testng reporter logs.
How to do it…
1) Write a test case for Sign In application and implement Log4j logging on every step.
2) Insert Reporter logs on the main events of the test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package automationFramework;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;

import utility.Log;

public class ReporterLogs {
  private static WebDriver driver;
  private static Logger Log = Logger.getLogger(Log.class.getName());
    @Test
  public static void test() {
      DOMConfigurator.configure("log4j.xml");
        driver = new FirefoxDriver();
        Log.info("New driver instantiated");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        Log.info("Implicit wait applied on the driver for 10 seconds");
        driver.get("https://www.facebook.com/");
        Log.info("Web application launched");
        // Our first step is complete, so we produce a main event log here for our reports.
        Reporter.log("Application Lauched successfully | ");
  
        driver.findElement(By.id("email")).sendKeys("yourusername");
        Log.info("Username entered in the Username and email text box");
        driver.findElement(By.id("pass")).sendKeys("yourpassword");
        Log.info("Password entered in the Password text box");
        driver.findElement(By.id("loginbutton")).click();
        Log.info("Click action performed on Submit button");
        // Here we are done with our Second main event
        Reporter.log("Sign In Successful | " );
        driver.quit();
        Log.info("Browser closed");
        // This is the third main event
        Reporter.log("User is Logged out and Application is closed | ");
  }

}
3) Run the test by right click on the test case script and select Run As > TestNG Test.
Your Log4j logging output will look like this:

But your Reporters log will look like this:

Log4j logging will help you to report a bug or steps taken during the test, on the other hand reporters log will help you to share the test status with leadership. As leadership is just interested in the test results, not the test steps.
I also use reporter’s logs on the verification during the test. For example
1
2
3
4
5
if(Text1.equals(Text2)){
      Reporter.log("Verification Passed forText");
  }else{
      Reporter.log("Verification Failed for Text");
  }
TestNG Asserts
TestNG also gives us the power to take decisions in the middle of the test run with the help of Asserts. With this we can put various checkpoints in the test. Asserts are the most popular and frequently used methods while creating Selenium Scripts. In selenium there will be many situations in the test where you just like to check the presence of an element. All you need to do is to put an assert statement on to it to verify its existence.
Different Asserts Statements
1) Assert.assertTrue() & Assert.assertFalse()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package automationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class Asserts {
  private static WebDriver driver;
  @Test
  public void f() {
    driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("https://www.facebook.com/");
     
      // Here driver will try to find out My username textbox on the application
      WebElement text_username = driver.findElement(By.id("email"));
     
      //Test will only continue, if the below statement is true
      //This is to check whether the textbox is displayed or not
      Assert.assertTrue(text_username.isDisplayed());
     
      //My username text will be type only if the above condition is true
      myAccount.sendKeys("your_username");
  }
}
Note: Assert true statement fails the test and stop the execution of the test, if the actual output is false. Assert.assertFalse() works opposite of Assert.assertTrue(). It means that if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.
2) Assert.assertEquals()
1
2
3
4
5
6
7
@Test
  public void test() {
    String sValue = "Ta bao Lan";
    System.out.println(" What is your full name");
    Assert.assertEquals("Ta Bao Lan", sValue);
    System.out.println(sValue);
  }
It also works the same way like assert true and assert fail. It will also stop the execution, if the value is not equal and carry on the execution, if the value is equal.

Hope you Like this post.


Saturday, December 27, 2014

Datepicker using Selenium WebDriver


In This Program :
  • Navigating to http://jqueryui.com/datepicker/
      

  • Clicking on TextBox By ID driver.findElement(By.id("datepicker")).click();
          

  • Click on next so that we will be in next month

   

  • Date Widget is a table so we taking the td and tr store it in List
        WebElement datewidget=driver.findElement(By.id("ui-datepicker-div"));

    List<WebElement> rows=datewidget.findElements(By.tagName("tr"));
       List<WebElement> columns=datewidget.findElements(By.tagName("td"));

  • Loop the Column and get the date
for(WebElement cell : columns) {

           if(cell.getText().equals("23"){
                            cell.findElement(By.linkText("23")).click();
  break;
 }}



Source code  :

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class datepick {
      
       WebDriver driver;
      
       @BeforeTest
       public void setup()
       {
              driver=new FirefoxDriver();
             
       }
       @Test
       public void testdate()
       {
               driver.get("http://jqueryui.com/datepicker/"); 
               driver.switchTo().frame(0); 
               driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
               driver.findElement(By.id("datepicker")).click();
               driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
               driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
               //datewidget is a table it contain rows and colums
               WebElement datewidget=driver.findElement(By.id("ui-datepicker-div"));
               List<WebElement> rows=datewidget.findElements(By.tagName("tr"));
               List<WebElement> columns=datewidget.findElements(By.tagName("td"));
               
               for(WebElement cell : columns)
               {
                      if(cell.getText().equals("23"))
                      {
                            cell.findElement(By.linkText("23")).click();
                            break;
                      }
               }
       }
      
       @AfterTest
       public void end()
       {
              driver.quit();
       }

}


Hope you Likke this post.