In this blog, we will discuss about a utility that allows us to read a Gmail mail body and then extract its content in spreadsheet using Selenium and Java.
But before that, we should know about the prerequisites that we will need to set on our machine for the successful execution of the gmail utility.
Tools and technologies used are following:
- Java
- Selenium web driver
- Spreadsheet-excel
- Maven build tool
- TestNG
- Docker
Set environment variable for the email, password and the mail subject to be searched which we will use in execution in Gmail utility.You can prefer to the link :https://www.winhelponline.com/blog/set-user-environment-variable-setx-windows-10/
Run the following command to set the user environment variable:
setx key “value”
Create a maven based project because we need to add dependencies in our project.
Add below dependencies to the file POM.xml :
POM.xml
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
Package com.scripts:
It contains different classes and hence performing functions like opening browser ,login Gmail etc.
- openBrowser.java : Helps to open the browser so that URL can be open execution .
- loginGmail.java: Helps to login the mail using the environment variables , searching the content using the subject . Extract the body of the respective mail.
- writeContent.java: Helps in creating and writing the content in the spreadsheet and hence storing the file in the given location.The name of the file is given in format of time execution.
Package com.utility :
It contains different methods used for interacting and performing actions on web locators.
1. getKey():It helps to extract the environment variable which are email, password and subject.
2. enterValue(): It helps to enter the values in the text box using driver commands and locators in selenium.
3. clickElement(): It helps to click an element using driver commands and locators in selenium.
4. getContent(): It helps to extract the mail body using driver commands and CSS locators in selenium.
5. takeScreenshot(): It helps to capture image of the execution and save the image at given location with the file name in execution time format.
Resources folder contains ObjectRepositories.properties file which store all the locators that are used in the utility.
openBrowser.java
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.utility.Utilities;
public class OpenBrowser {
public static WebDriver driver;
@BeforeTest
public static void OpenGmail() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName(BrowserType.CHROME);
driver = new RemoteWebDriver(newURL("http://192.168.99.100:4646/wd/hub"), cap);
driver.get("https://gmail.com");
/****** For using browser launch using selenium driver ***********/
/*
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); driver= new ChromeDriver(); driver.manage().timeouts().pageLoadTimeout(30,
TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS); driver.manage().window().maximize();
driver.get("https://gmail.com");
*/
}
}
loginGmail.java
import org.testng.annotations.Test;
import com.utility.Utilities;
public class LoginGmail extends OpenBrowser {
@Test
public static void loginSearchContent() throws InterruptedException, IOException {
/** Entered credentials **/
Utilities.WaitforElement();
Utilities.EnterValues("login.email", "Email");
Utilities.ClickElement("login.next");
Utilities.WaitforElement();
Utilities.EnterValues("login.password", "Password");
Utilities.ClickElement("login.passwordnext");
Utilities.TakeScreenshot();
/**** Mail search through subject ****/
Utilities.WaitforElement();
Utilities.TakeScreenshot();
Utilities.EnterValues1("mail.subject", "Subject");
Utilities.WaitforElement();
Utilities.TakeScreenshot();
Utilities.ClickElement("mail.mail");
Utilities.TakeScreenshot();
Utilities.WaitforElement();
/***** Getting content ******/
Utilities.TakeScreenshot();
Utilities.WaitforElement();
Utilities.GetContent();
Utilities.TakeScreenshot();
System.out.println(Utilities.GetContent());
}
}
writeContent.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;
import com.utility.Utilities;
public class WriteContent {
XSSFWorkbook workbook;
XSSFSheet sheet;
FileOutputStream file;
Row row;
@Test
public void createExcel() throws IOException {
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("ContentSheet");
/**** name of the sheet to be according to the time ***/
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime now = LocalDateTime.now();
String Timegiven = dtf.format(now);
String Time = Timegiven.replace(":", "");
OutputStream files = new FileOutputStream(
new File(System.getProperty("user.home") + "\\Desktop\\" + Time + "content.xlsx"));
String content = Utilities.GetContent();
String Para[] = content.split("((\\n\\r)|(\\r\\n)){2}|(\\r){2}|(\\n){2}");
for (int k = 0; k < Para.length; k++) {
System.out.println(Para[k]);
writeExcel(Para[k], k);
}
}
public void writeExcel(String data, int j) throws IOException {
Row row = sheet.createRow(j);
Cell cell = row.createCell(0);
cell.setCellValue(data);
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime now = LocalDateTime.now();
String Timegiven = dtf.format(now);
String Time = Timegiven.replace(":", "");
FileOutputStream file = new FileOutputStream(
new File(System.getProperty("user.home") +
"\\Desktop\\" + Time + "content.xlsx"));
workbook.write(file);
}
}
utilities.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.lang.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import com.scripts.OpenBrowser;
import static com.scripts.OpenBrowser.*;
public class Utilities {
public static String Content;
public static String GetKey(String key)
{
String value = System.getenv(key);
return value;
}
public static void EnterValue(String key, String key2) throws FileNotFoundException {
Properties propmain = new Properties();
FileInputStream FileInputStream = new FileInputStream(
new File(System.getProperty("user.dir") + "\\src\\main\\resources\\ObjectRepository.properties"));
try {
propmain.load(FileInputStream);
String locator = propmain.getProperty(key);
String value = GetKey(key2);
OpenBrowser.driver.findElement(By.id(locator)).sendKeys(value);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void ClickElement(String key) throws FileNotFoundException {
Properties propmain = new Properties();
FileInputStream FileInputStream = new FileInputStream(
new File(System.getProperty("user.dir") + "\\src\\main\\resources\\ObjectRepository.properties"));
try {
propmain.load(FileInputStream);
String locator = propmain.getProperty(key);
OpenBrowser.driver.findElement(By.xpath(locator)).click();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void WaitforElement() throws FileNotFoundException, InterruptedException {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public static void EnterValues1(String key, String key2) throws FileNotFoundException {
Properties propmain = new Properties();
FileInputStream FileInputStream = new FileInputStream(
new File(System.getProperty("user.dir") + "\\src\\main\\resources\\ObjectRepository.properties"));
try {
propmain.load(FileInputStream);
String locator = propmain.getProperty(key);
String value = GetKey(key2);
OpenBrowser.driver.findElement(By.xpath(locator)).sendKeys(value); OpenBrowser.driver.findElement(By.xpath(locator)).sendKeys(Keys.ENTER);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void EnterValues(String key, String key2) throws FileNotFoundException {
Properties propmain = new Properties();
FileInputStream FileInputStream = new FileInputStream(
new File(System.getProperty("user.dir") + "\\src\\main\\resources\\ObjectRepository.properties"));
try {
propmain.load(FileInputStream);
String locator = propmain.getProperty(key);
String value = GetKey(key2);
OpenBrowser.driver.findElement(By.xpath(locator)).sendKeys(value);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void Enter(String key) throws FileNotFoundException {
Properties propmain = new Properties();
FileInputStream FileInputStream = new FileInputStream(
new File(System.getProperty("user.dir") + "\\src\\main\\resources\\ObjectRepository.properties"));
try {
propmain.load(FileInputStream);
String locator = propmain.getProperty(key);
OpenBrowser.driver.findElement(By.id(locator)).sendKeys("Keys.ENTER");
} catch (IOException e) {
e.printStackTrace();
}
}
public static String GetContent() throws FileNotFoundException {
Content =
OpenBrowser.driver.findElement(By.cssSelector(".ii.gt")).getText();
return Content;
}
public static void TakeScreenshot() throws IOException {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String Timegiven = dtf.format(now);
String Time = Timegiven.replace(":", "");
TakesScreenshot ts1 = (TakesScreenshot) driver;
File source1 = ts1.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source1, new File((System.getProperty("user.home") + "\\Desktop\\" + Time + "Picture.png")));
}
}
objectRepository.properties
login.email=//input[@id='identifierId']
login.next=//div[@id='identifierNext']
login.password=//input[@name='password']
login.passwordnext=//div[@id='passwordNext' or @id='submit']
mail.subject=//input[@placeholder='Search mail']
mail.mail=(//div[contains(text(),'Inbox')])[1]
We need to download and install docker on your system .
Now to run selenium code on chrome browser you need to run following commands:
docker pull selenium/hub
docker pull selenium/node-chrome-debug
Use following commands to generate container ID:
docker run -d -P –link selenium-hub:hub selenium/node-chrome-debug
docker run -d -p 4545:4444 –name selenium-hub selenium/hub (Here 4545 is the port number you want to have to run the execution you can give value).
When these commands are successfully executed check the container ID using docker ps –a
It will show the container ID,Image name,created time ,status and the ports and we can check every field.
Now we can check whether the URL is working or not using the port number and hit the URL on chrome browser.
For Linux:
URL:localhost:4646/wd/hub
For windows :
Run command: docker-machine ip
URL: {docker-machine ip}/wd/hub
Run this URL with the port number on the chrome browser since it will help you to know the docker setting is working.The URL will be launched on the browser.
Now in openBrowser.java class,we replace the URL according to the port number
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName(BrowserType.CHROME); driver = new RemoteWebDriver(new
URL("http://192.168.99.100:4646/wd/hub"), cap);
Now run the code and then we will get the desired output files in the form of spreadsheet and the screenshots for execution.