Friday, 25 January 2019

Email Attachment in Selenium through Gmail ID using Apache Commons Email Concept


1)To create a Maven Projet.

2)Add the Dependency Commons-Email in POM.xml file.

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.3.1</version>
</dependency>

3.Create a class and implement the code
package Sampletester;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class NewTestEmailForm {

     public static void main(String[] args) throws EmailException {
           // TODO Auto-generated method stub
             EmailAttachment attachment = new EmailAttachment();
             attachment.setPath("D:\\test11.png");
             attachment.setDisposition(EmailAttachment.ATTACHMENT);
             attachment.setDescription("Picture of test");
             attachment.setName("test report");

             // Create the email message
             MultiPartEmail email = new MultiPartEmail();
             email.setHostName("smtp.gmail.com");
             email.setSmtpPort(465);
             email.setAuthenticator(new DefaultAuthenticator("yourgmailID@gmail.com", "yourgmailpassword"));
             email.setSSLOnConnect(true);
             email.setFrom("yourgmailID@gmail.com");
             email.setSubject("TestMail Attachment");
             email.setMsg("This is a test mail ...for attahment:-)");
             email.addTo("reeiveremail address@gmail.com");
            

             // add the attachment
             email.attach(attachment);

             // send the email
             email.send();
            
             System.out.println("report");
     }

}


4.Open the Gmail Account and Check your Attachment.


Take Screenshot of Entire Page using Shutterbug in Selenium WebDriver 3


Scenario 1:
-----------
When Try with selenium-server-standalone-3.14.jar to take the screenshot.

Refer the code:
---------------

File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\path\\screenshot.png"));

Scenario 2:
-----------

When Try with selenium-server-standalone-3.141.59(Latest Version).jar to take the screenshot.

It doesnot support the FileUtils.copyFile replace that it support Files.copy

File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
Files.copy(scrFile, new File("D:\\path\\screenshot.png"));

Note:
Both scenario 1 and scenario 2 it does not take the screenshot of entire page.

Scenario 3:
-----------
To Take the Entire page of screenshot.

1)To create a Maven Projet.

2)Add the Dependency Selenium-shutterbug in POM.xml file.

<!-- https://mvnrepository.com/artifact/com.assertthat/selenium-shutterbug -->
<dependency>
    <groupId>com.assertthat</groupId>
    <artifactId>selenium-shutterbug</artifactId>
    <version>0.9</version>
</dependency>

3)Create a Class and Implement the Code.

package Screen;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.assertthat.selenium_shutterbug.core.Shutterbug;
import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy;

public class Take {

public static void main(String[] args) {
           // TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\chromedriver_win32\\chromedriver.exe");
       
WebDriver d = new ChromeDriver();
d.get("https://www.seleniumhq.org/download/");
       Shutterbug.shootPage(d,ScrollStrategy.BOTH_DIRECTIONS,500,true).withName("FullPageScreenshot").save();

        d.quit();
     }

}

4)After exeute the code Right click the project folder and refersh it then it generate the folder "Screenshot" and open your screenshot file.


Output of the Program:





       
       
       

          
    



Wednesday, 3 October 2018

TestNG-Introduction-Basic Program

TestNG-Next Generation

1)Using TestNG to test the Application as Module by Module.

2)It is not a In build Framework,need to Install Sepreately in Eclipse

3)To Test the Modules as in Priority Order.

4)It Support Data Driven Testing(To Pass a Inputs in Program Itself).

5)It Support Parallel Testing.(Run the Application in Different Browser's in Same Time).

6)It generate the Test Reports(after execute the Program Refresh the Project)-It generate the Test folder.

Note:Right Click the Project-->Press the Option Refresh.

7)There is No Main Method in TestNG.(Replace that using the Annotation Concept).

Types of Annotation:

Basic Annotation:
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass

@BeforeTest
@AfterTest

For Data Driven Testing:

@DataProvider

For Parallel Testing:

@Parameters

Sample Program for TestNG:

Basic Annotations: 
package SampleTestNgPgm;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class NewTest {
@Test
public void f1() {
System.out.println("functional test");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("open url");

}

@AfterMethod
public void afterMethod() {

System.out.println("close url");

}

@BeforeClass
public void beforeClass() {
System.out.println("open browser");
}

@AfterClass
public void afterClass() {
System.out.println("close browser");
}

}

Output of the Program:



Note:Right Click the Project-->Press the Option Refresh and Open the index.html


Test a Multiple Checkbox with in Single Click

To Test a Multiple Checkbox with in Single Click.

When the Checkbox element contains with Same (Name-Locator).



WebDriver t=new ChromeDriver();

t.get("url");

List<WebElement> s=t.findElements(By.name("email_to[]")));

System.out.println(s.getSize());

for(int i=1;i<s.getSize();i++)
{
s.get(i).click(); ---->Select the Dropdown

s.get(i).click(); ---->Deselect the Dropdown
}



Sunday, 23 September 2018

Serenity configuration and Execution

Serenity configuration

 Add Serenity .jar files in pom.xml (will download into eclipse IDE)
-          Serenity – core
-          Serenity – cucumber
-          Serenity -Junit

Code snippet:
<dependency>
              <groupId>net.serenity-bdd</groupId>
              <artifactId>serenity-core</artifactId>
              <version>1.9.31</version>
</dependency>          
<dependency>
              <groupId>net.serenity-bdd</groupId>
              <artifactId>serenity-cucumber</artifactId>
              <version>1.9.12</version>
</dependency>
<dependency>
              <groupId>net.serenity-bdd</groupId>
              <artifactId>serenity-junit</artifactId>
              <version>1.9.31</version>
</dependency>
2.1   Add junit .jar files into pom.xml
<dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-simple</artifactId>
              <version>1.6.1</version>
</dependency>
<dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
              <scope>test</scope>
       </dependency>
       <dependency>
              <groupId>org.assertj</groupId>
              <artifactId>assertj-core</artifactId>
              <version>1.7.0</version>
              <scope>test</scope>
</dependency>
Add property file – (version of serenity and web driver)
<properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <serenity.version>1.0.47</serenity.version>
              <serenity.maven.version>1.0.47</serenity.maven.version>
              <webdriver.driver>chrome</webdriver.driver>
</properties>

2.1   Add Report generated build files
<build>
              <plugins>
                     <plugin>
                           <artifactId>maven-failsafe-plugin</artifactId>
                           <version>2.18</version>
                           <configuration>
                                  <includes>
                                         <include>**/*TestRunner.java</include>
                                  </includes>
                                  <systemProperties>
                                         <webdriver.driver>chrome</webdriver.driver>
                                         <surefire.rerunFailingTestsCount>5</surefire.rerunFailingTestsCount>
                                         <surefire.rerunFailingTestsCount>5</surefire.rerunFailingTestsCount>
                                  </systemProperties>
                           </configuration>
                     </plugin>
                     <plugin>
                           <groupId>net.serenity-bdd.maven.plugins</groupId>
                           <artifactId>serenity-maven-plugin</artifactId>
                           <version>1.0.6</version>
                           <dependencies>
                                  <dependency>
                                         <groupId>net.serenity-bdd</groupId>
                                         <artifactId>core</artifactId>
                                         <version>1.0.47</version>
                                  </dependency>
                           </dependencies>
                           <executions>
                                  <execution>
                                         <id>serenity-reports</id>
                                         <phase>post-integration-test</phase>
                                         <goals>
                                                <goal>aggregate</goal>
                                         </goals>
                                  </execution>
                           </executions>
                     </plugin>
              </plugins>
       </build>

Creating a project using Serenity – Cucumber BDD – Maven

Create a Maven project in Eclipse

Configure the serenity and cucumber jar files in pom.xml
Refer Serenity configuration section (pom.xml)
 Create the Feature file under the project
Feature folder -> Filename.feature  (will write scenarios)

Copy the Browser .exe files (IE/Chrome/Safari/Firefox) into project workspace

Create packages and classes (Homepage.java – URL and BuyerSteps.java – invoke browser and StepDef.java -  feature file generated methods) under the project 

Process of execution:
-          Run Feature file as 
-          Once feature file runs, will generate the methods and copy the generated methods into the stepdefinition class.
-          Maven build and maven test

-          Run the Test Runner class

Once successful execution of scrip, the results will be pass and browser will close


The pom file

Herewith attached the complete .jar set up

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Testserenity</groupId>
<artifactId>Testserenity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Testserenity</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<serenity.version>1.0.47</serenity.version>
<serenity.maven.version>1.0.47</serenity.maven.version>
<webdriver.driver>chrome</webdriver.driver>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-core -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>1.9.31</version>
</dependency>



<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-cucumber -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>1.9.12</version>
</dependency>



<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>1.9.31</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<configuration>
<includes>
<include>**/*TestRunner.java</include>
</includes>
<systemProperties>
<webdriver.driver>chrome</webdriver.driver>
<surefire.rerunFailingTestsCount>5</surefire.rerunFailingTestsCount>
<surefire.rerunFailingTestsCount>5</surefire.rerunFailingTestsCount>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId>
<version>1.0.6</version>
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>core</artifactId>
<version>1.0.47</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>serenity-reports</id>
<phase>post-integration-test</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Issues Facing During Serenity Configuration

  •   While build running the serenity in Eclipse IDE we should make to add serenity cucumber dependency in pom.xml file since we’ve separate serenity cucumber dependency.
  •   Make sure the Feature folder name same as the name mentioned in the TestRunner class.
  • The name of the stepdefinition assigned to glue will be same as the name of the stepdefinition in Test Runner class i.e., @cucumberOptions (feature = “Feature”,   Glue = {“stepdefinition”})
  • Serenity.properties : Invoking test execution browsers should be mentioned in the serenity.properties file.
  •  Make sure that browser .exe file should be placed in project work space.
  • Whenever we’re updating the any dependencies through pom.xml, make sure that pom file should be cleaned (Run As -> Maven clean => will appear a message successfully build file cleaned) and then pom file should build (Run As -> Maven Build).
  • Clean the Project in the project workspace, if we do multiple updates in pom.xml file or serenity.properties file etc.,