Automation Using Selenium Webdriver

Monday, 31 October 2016

How to work with Firefox browser using Selenium 3.0 beta 1

How to work with Firefox browser using Selenium 3.0 beta 1

Recently selenium has launched Selenium 3.0 beta 1 jar.In this post I will show you How to work with Firefox browser using Selenium 3.0 beta 1 jar.

Please follow the below steps:
Step:1
Just like the other drivers available to Selenium from other browser vendors, Mozilla has released now an executable that will run alongside the browser

In order to work with Firefox browser using Selenium 3 beta 1 jar, you need to use separate a driver which will interact with Firefox browser called as "Geckodriver".

Please find below link for downloading latest version of geckodriver.

https://github.com/mozilla/geckodriver/releases/download/v0.9.0/geckodriver-v0.9.0-win64.zip

Step:2
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");

Step:3
driver = new FirefoxDriver();

Note: Still if you are using old versions of selenium jars(selenium 2) then you can skip first two steps.

Sample Code:
public class SampleTest {

 public WebDriver driver;

 @Test
 public void setup(){

  System.setProperty("webdriver.gecko.driver", "E:/geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://www.facebook.com/");

  }

}

Sunday, 30 October 2016

Email the selenium automation reports through SMTP

Email the selenium automation reports through SMTP
In this post I am going to explain how can we Email the reports after Selenium test execution
with the help of Java using java Mail libraries.

Download Javax Mail jar libraries in following link  and configure to your project.
Download Javax Mail Jar

In this post we are going to send Email through Gmail. So we have to configure SMTP settings of it.Please find the below SMTP Settings of Gmail.

Gmail SMTP settings

Please find the sample code for the same.

Sample Code:

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
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 SendMailUtility {


 //Send mail
 public  boolean sendMail(String userName,
        String passWord,
        String host,
        String port,
        String starttls,
        String auth,
        boolean debug,
        String socketFactoryClass,
        String fallback,
        String[] to,
        String[] cc,
        String[] bcc,
        String subject,
        String text,
        String attachmentPath,
        String attachmentName){
              Properties props = new Properties();
        props.put("mail.smtp.user", userName);
        props.put("mail.smtp.host", host);
               if(!"".equals(port))
        props.put("mail.smtp.port", port);
                if(!"".equals(starttls))
        props.put("mail.smtp.starttls.enable",starttls);
        props.put("mail.smtp.auth", auth);
              if(debug){
              props.put("mail.smtp.debug", "true");
              }else{
              props.put("mail.smtp.debug", "false");      
              }
              if(!"".equals(port))
        props.put("mail.smtp.socketFactory.port", port);
                if(!"".equals(socketFactoryClass))
        props.put("mail.smtp.socketFactory.class",socketFactoryClass);
                if(!"".equals(fallback))
        props.put("mail.smtp.socketFactory.fallback", fallback);
        try
        {
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(debug);
            //attachment start        
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source =
            new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(
            new DataHandler(source));
            messageBodyPart.setFileName(attachmentName);
         
            //Message body
            messageBodyPart.addHeaderLine("Please find the attachement");
            multipart.addBodyPart(messageBodyPart);
       
            // Attachment ends
             MimeMessage msg = new MimeMessage(session);

            msg.setSubject(subject);
            msg.setContent(multipart);
       
            //msg.setText(text);
            System.out.println("text================>"+text);
            msg.setFrom(new InternetAddress(userName));

                        for(int i=0;i<to.length;i++){
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));

                        }
                        for(int i=0;i<cc.length;i++){
            msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
                        }
                        for(int i=0;i<bcc.length;i++){
            msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
                        }
            msg.saveChanges();
                        Transport transport = session.getTransport("smtp");
                        transport.connect(host, userName, passWord);
                        transport.sendMessage(msg, msg.getAllRecipients());
                        transport.close();

                        return true;
        }
        catch (Exception mex)

        {
            mex.printStackTrace();

            return false;
        }

 }

 //usage
 public static void main(String[] args){

  SendMailUtility utility = new SendMailUtility();
  //You can send to many number of members at a time by separating comma
  String[] to={"testingteam@gmail.com","tl@gmail.com"};
  String[] cc={"manager@gmail.com"};
        String[] bcc={"testingteam@gmail.com@gmail.com"};

  utility.sendMail(
    "Sampletest@gmail.com",
            "Test@123",
             "smtp.gmail.com",
             "465",
             "true",
             "true",
             true,
             "javax.net.ssl.SSLSocketFactory",
             "false",
             to,
             cc,
             bcc,
             "Automation Reports",                            
             "Please find the attached reports",
             "E:/WorkSpace/Practice/AutomationReports.zip",
             "AutomationReports.zip");

     System.out.println("Report has been sent through mail Successfully");
 
}
}

Friday, 28 October 2016

Cross Browser Testing using Selenium WebDriver

Cross Browser Testing using Selenium WebDriver

In this post I am going to explain how can we do Cross Browser Testing using Selenium.

If we are using Selenium WebDriver,In order to execute test cases using different browsers like Firefox, Chrome, Opera ,Internet Explorer we have to use TestNG framework.

Test.xml :
In TestNG, if we want execute same test case with different browsers we have to use Test.xml file.
Sample Code:
<suite name="CrossBrowserTesting" preserve-order="true" parallel="false" >
 
<test name="Smoketest" preserve-order="true">

<parameter name="browserName" value="firefox"></parameter>
 <!--
 <parameter name="browserName" value="ie"></parameter>
 <parameter name="browserName" value="chrome"></parameter>
 <parameter name="browserName" value="opera"></parameter>
  -->
<classes>

<class name="Configuration.HMSLogin"></class>
   
</classes>

</test>

</suite>

BrowserType.java
We have to pass "browserName" parameters from Test.xml and it will map with the BrowserType.java using @Parameters  annotation.
And the BrowserType.java will look like below
Sample Code:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class BrowserType {

 public static WebDriver driver;
 @BeforeClass
 @Parameters("browserName")
 public void setup(String browserName) throws IOException, InterruptedException{

  //open Firefox Browser
  //open the browser based on parameter passed in .xml file.
  if(browserName.equalsIgnoreCase("firefox")){
 
   //if you are using selenium 2.0
   driver = new FirefoxDriver();
 
   //if you are using selenium 3.0
   //System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
   //driver = new FirefoxDriver();
  }
  else if(browserName.equalsIgnoreCase("chrome")){
 
   //open chrome browser
   System.setProperty("webdriver.chrome.driver", "E:/Selenium Training/Driver Files/chromedriver.exe");
   driver= new ChromeDriver();
  }

  else if(browserName.equalsIgnoreCase("ie")){
   //open Internet Explorer Browser
   System.setProperty("webdriver.ie.driver", "E:/Selenium Training/Driver Files/IEDriverServer.exe");
   driver= new InternetExplorerDriver();
 
  }
  else if(browserName.equalsIgnoreCase("opera")){
   //open opera Browser
   System.setProperty("webdriver.opera.driver", "E:/Selenium Training/Driver Files/operadriver.exe");
   driver= new OperaDriver();
 
  }
  //maximizing window
  driver.manage().window().maximize();

  //Navigating URL
  driver.get("http://www.facebook.com/");

  }
}

Testcase:
We have to extends BrowserType class to each test-suite and it will look like below
Sample Testcase code:
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class HMSLogin extends BrowserType {
 @Test(priority=1)
 public void Login() {

  driver.findElement(By.xpath("//input[@name='username']")).clear();
  driver.findElement(By.xpath("//input[@name='username']")).sendKeys("admin");

  driver.findElement(By.xpath("//input[@name='password']")).clear();
  driver.findElement(By.xpath("//input[@name='password']")).sendKeys("admin");

  driver.findElement(By.xpath("//input[@name='submit']")).click();

}

}


finally execute the Test-suite by using Test.xml file.