Automation Using Selenium Webdriver

Saturday, 15 October 2016

How To Get Height And Width Of Element In Selenium WebDriver

As you know, Every web element has Its own height and width or we can say size element. Sometimes you need to perform pixel to pixel testing as per client's requirement like each and every button's height should be x(some pixels) and width should be y(some pixels) through out the site. 

Now If you go for measuring height and width of every element manually then It Is time consuming on every test cycle. Automation Is best solution to measure height and width of elements because It Is one time exercise and then you can use It In every test cycle.
We have learnt how to get x y coordinates of element using selenium web driver In previous post. Now let us learn how to get size of element. I have prepared simple example to explain
 you how to get height and width of Image In selenium web driver. As you can see In bellow example, I have used getSize() method to get height and width of element.

package Testing_Pack;

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.BeforeTest;
import org.testng.annotations.Test;

public class getElementSize {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://any web page");
 }

 @Test
 public void getSize() throws Exception {
  //Locate element for which you wants to get height and width.
        WebElement Image = driver.findElement(By.xpath("//img[@border='0']"));
        
        //Get width of element.
        int ImageWidth = Image.getSize().getWidth();
        System.out.println("Image width Is "+ImageWidth+" pixels");
        
        //Get height of element.
        int ImageHeight = Image.getSize().getHeight();        
        System.out.println("Image height Is "+ImageHeight+" pixels");
 }
}

When you run above test In eclipse, It will print height and width of
 element In console as bellow.
OutPut::
Image width Is 212 pixels
Image height Is 191 pixels

POM(Page Factory)Advantages Example


PAGE FACTORY :


How To Set/Get Window Position And Size In Selenium WebDriver

Sometimes you need to set window position and size or get window size and position In selenium software test. Selenium webdriver software testing tool has many useful methods using which we can play with web browser window for different purpose. One of them Is window().maximize(); which we use Ineach and every webdriver software test to maximizing the window. Same way,webdriver has window size and position related Independent methods.

What Is window size and position?
Before learning how to set/get size and position, We need tounderstand It.
Window size means height and width of window.
Window position means distance of window from left side(X Coordinates) of screen and top side(Y Coordinates) of screen.
Bellow given Image describes you more about window size and
 position.





window().setSize() to set window size
We can use window().setSize() method to set the size of window In selenium webdriver software test. We can set window width to 300 and height to 500 dimensions using bellow given syntax In selenium webdriver.driver.manage().window().setSize(new Dimension(300,500));

window().getSize() to get window sizeWe can use window().getSize() method to get size of window.
Bellow given syntax will return window height using getSize().getHeight().driver.manage().window().getSize().getHeight()

Bellow given syntax will return window width using getSize().getWidth().
driver.manage().window().getSize().getWidth()window().setPosition() to set position of window
We can set window position to 50 points from left side and 200 points from top side using bellow given syntax.
driver.manage().window().setPosition(new Point(50,200));

window().getPosition() to get position of window Use window().getPosition().getX() to get window position from left side as bellow.
driver.manage().window().getPosition().getX()

To get window position from top side of screen, use window().getPosition().getY() In your software test as bellow.
driver.manage().window().getPosition().getY()

Full webdriver test to explore all above methods of webdriver window Is as bellow.

package Testing_Pack;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class WindowSizePosition {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 }

 @Test(priority=1)
 public void setGetWinSize(){
  //WebDriver setSize method used to set window size width = 300 and height = 500.
  driver.manage().window().setSize(new Dimension(300,500));
  
  //WebDriver getSize method used to get window width and height.
  System.out.println("Window height Is -> "+driver.manage().window().getSize().getHeight());
  System.out.println("Window width Is -> "+driver.manage().window().getSize().getWidth());
 }

 @Test(priority=2)
 public void setGetWinPosition(){
  //WebDriver setPosition method used to set window position x coordinate = 50 and y coordinate =100.
  driver.manage().window().setPosition(new Point(50,200));
  
  //WebDriver getPosition method used to get window position x,y coordinates.
  System.out.println("Window position X coordinates Is -> "+driver.manage().window().getPosition().getX());
  System.out.println("Window position Y coordinates Is "+driver.manage().window().getPosition().getY());
 }
}



Friday, 14 October 2016

How to get from dropdown List in Cities



Print all dropdown cities www.goindigo.com

package com.Test.web;

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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PrintCitesDropDown {
    static WebDriver driver;
public static void main(String[] args) throws Exception {
Thread.sleep(5000);
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
// driver=new FirefoxDriver();
driver=new ChromeDriver();
driver.manage().window().maximize();
//Print DropDown list Cities
driver.get("https://www.goindigo.in/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[@id='roundWay']/form/ul/li[1]/input[1]")).click();
List<WebElement>cities=driver.findElements(By.xpath(".//*[@id='roundWay']/form/ul/li[1]/div/ul/li"));
System.out.println("Number of cities:"+cities.size());
for (int i = 0; i < cities.size(); i++) {
System.out.println(i+1+")city name:"+cities.get(i).getText());

}

}

}
OUTPUT::

How to Send report through email in Selenium Webdriver

Do you know that we can Send report through email in Selenium Webdriver with small code with help of additional jars?
Today I will show you how you can Send report through email in Selenium Webdriver using simple steps and trust me this is one of the most important features that you should include in your framework as well.
It does not matter which framework you are using it could be Keyword driver, Data driven and Hybrid you should implement email report functionality after test execution.

Step by Step process to Send report through email in Selenium Webdriver

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMailSSLWithAttachment {
public static void main(String[] args) {
// Create object of Property file
Properties props = new Properties();
// this will set host of server- you can change based on your requirement
props.put("mail.smtp.host", "smtp.gmail.com");
// set the port of socket factory
props.put("mail.smtp.socketFactory.port", "465");
// set socket factory
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// set the authentication to true
props.put("mail.smtp.auth", "true");
// set the port of SMTP server
props.put("mail.smtp.port", "465");
// This will handle the complete authentication
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("add your email", "add your password");
}
});
try {
// Create object of MimeMessage class
Message message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress("thotajagan63@gmail.com"));
// Set the recipient address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("jagan@gmail.com"));
            
                        // Add the subject link
message.setSubject("Testing Subject");
// Create object to add multimedia type content
BodyPart messageBodyPart1 = new MimeBodyPart();
// Set the body of email
messageBodyPart1.setText("This is message body");
// Create another object to add another content
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
// Mention the file which you want to send
String filename = "G:\\a.xlsx";
// Create data source and pass the filename
DataSource source = new FileDataSource(filename);
// set the handler
messageBodyPart2.setDataHandler(new DataHandler(source));
// set the file
messageBodyPart2.setFileName(filename);
// Create object of MimeMultipart class
Multipart multipart = new MimeMultipart();
// add body part 1
multipart.addBodyPart(messageBodyPart2);
// add body part 2
multipart.addBodyPart(messageBodyPart1);
// set the content
message.setContent(multipart);
// finally send the email
Transport.send(message);
System.out.println("=====Email Sent=====");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}