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: