Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be5cdab

Browse files
add page object framework
1 parent 4d1b0f5 commit be5cdab

File tree

4 files changed

+171
-79
lines changed

4 files changed

+171
-79
lines changed

‎src/test/java/com/crossover/e2e/GMailTest.java

Lines changed: 13 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.io.File;
44
import java.io.FileReader;
55
import java.util.Properties;
6+
7+
import com.crossover.pages.GmailPage;
8+
import com.crossover.pages.LoginPage;
69
import junit.framework.TestCase;
710
import org.junit.Test;
811
import org.openqa.selenium.*;
@@ -23,7 +26,7 @@ public void setUp() throws Exception {
2326
System.setProperty("webdriver.chrome.driver",properties.getProperty("webdriver.chrome.driver") );
2427
driver = new ChromeDriver();
2528
driver.manage().window().maximize();
26-
wait = new WebDriverWait(driver,20);
29+
wait = new WebDriverWait(driver,Integer.parseInt(properties.getProperty("timeout")));
2730
}
2831

2932
public void tearDown() {
@@ -34,85 +37,13 @@ public void tearDown() {
3437
public void testSendEmail() {
3538
driver.get("https://mail.google.com/");
3639

37-
//Enter User Name read from the properties file
38-
WebElement userElement = driver.findElement(By.id("identifierId"));
39-
String userName = properties.getProperty("username");
40-
userElement.sendKeys(userName);
41-
42-
//Click next
43-
driver.findElement(By.id("identifierNext")).click();
44-
45-
//Enter Password read from the properties file
46-
By passwordElementIdentifier = By.name("password");
47-
wait.until(ExpectedConditions.presenceOfElementLocated(passwordElementIdentifier));
48-
WebElement passwordElement = driver.findElement(passwordElementIdentifier);
49-
String passwordValue = properties.getProperty("password");
50-
passwordElement.sendKeys(passwordValue);
51-
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("passwordNext")));
52-
driver.findElement(By.id("passwordNext")).click();
53-
54-
//Click Compose
55-
By composeElementIdentifier = By.xpath("//*[@role='button' and (.)='Compose']");
56-
wait.until(ExpectedConditions.presenceOfElementLocated(composeElementIdentifier));
57-
driver.findElement(composeElementIdentifier).click();
58-
59-
By toFieldIdentifier = By.name("to");
60-
wait.until(ExpectedConditions.presenceOfElementLocated(toFieldIdentifier));
61-
WebElement txtBoxToField = driver.findElement(toFieldIdentifier);
62-
txtBoxToField.clear();
63-
String toUserValue = properties.getProperty("username");
64-
txtBoxToField.sendKeys(String.format("%s@gmail.com",toUserValue));
65-
66-
// emailSubject and emailbody to be used in this unit test.
67-
String emailSubject = properties.getProperty("email.subject");
68-
String emailBody = properties.getProperty("email.body");
69-
70-
//Enter subject
71-
By subjectBoxIdentifier = By.name("subjectbox");
72-
wait.until(ExpectedConditions.presenceOfElementLocated(subjectBoxIdentifier));
73-
WebElement subjectBox = driver.findElement(subjectBoxIdentifier);
74-
subjectBox.clear();
75-
subjectBox.sendKeys(emailSubject);
76-
77-
//Enter email body
78-
driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).clear();
79-
driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).sendKeys(emailBody);
80-
81-
//Click More settings
82-
wait.until(ExpectedConditions.presenceOfElementLocated(
83-
By.xpath("//div[@class='J-JN-M-I J-J5-Ji Xv L3 T-I-ax7 T-I']/div[2]"))).click();
84-
wait.until(ExpectedConditions.presenceOfElementLocated(
85-
By.xpath("//span[@class='J-Ph-hFsbo']"))).click();
40+
LoginPage loginPage = new LoginPage(driver, wait, properties);
41+
loginPage.login();
8642

87-
//Choose Social label
88-
wait.until(ExpectedConditions.presenceOfElementLocated(
89-
By.xpath("//div[@class='J-LC-Jz' and text()='Social']/div[@class='J-LC-Jo J-J5-Ji']"))).click();
90-
wait.until(ExpectedConditions.presenceOfElementLocated(
91-
By.xpath("//div[@class='J-JK-Jz' and text()='Apply']"))).click();
92-
93-
//Click Send
94-
driver.findElement(By.xpath("//*[@role='button' and text()='Send']")).click();
95-
96-
//Click Social Tab
97-
wait.until(ExpectedConditions.presenceOfElementLocated(
98-
By.xpath("//div[@class='aKz' and text()='Social']"))).click();
99-
100-
//Thread.sleep(5000);
101-
wait.until(ExpectedConditions.presenceOfElementLocated(
102-
By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")));
103-
104-
try{
105-
driver.findElement(By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")).click();
106-
}catch (ElementNotVisibleException e){
107-
driver.findElements(By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")).get(1).click();
108-
}
109-
110-
//Open the received email
111-
try {
112-
driver.findElement(By.xpath("//table[@class='F cf zt']/tbody/tr[1]")).click();
113-
}catch (ElementNotVisibleException e){
114-
driver.findElements(By.xpath("//table[@class='F cf zt']/tbody/tr[1]")).get(1).click();
115-
}
43+
GmailPage gmailPage = new GmailPage(driver,wait,properties);
44+
gmailPage.composeEmail();
45+
gmailPage.clickSocialTab();
46+
gmailPage.openReceivedEmail();
11647

11748
//Verify email came under proper Label i.e. "Social"
11849
try{
@@ -128,10 +59,13 @@ public void testSendEmail() {
12859
//Verify the subject and body of the received email
12960
String subjectOfReceivedEmail = wait.until(ExpectedConditions.presenceOfElementLocated(
13061
By.xpath("//h2[@class='hP']"))).getText();
62+
63+
String emailSubject = properties.getProperty("email.subject");
13164
assertEquals(emailSubject, subjectOfReceivedEmail);
13265

13366
String bodyOfReceivedEmail = wait.until(ExpectedConditions.presenceOfElementLocated(
13467
By.xpath("//div[@class='a3s aXjCH ']"))).getText();
68+
String emailBody = properties.getProperty("email.body");
13569
assertEquals(emailBody, bodyOfReceivedEmail);
13670
}
13771
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.crossover.pages;
2+
3+
import org.openqa.selenium.WebDriver;
4+
import org.openqa.selenium.support.ui.WebDriverWait;
5+
6+
import java.util.Properties;
7+
8+
public class BasePage {
9+
10+
protected final WebDriver driver;
11+
protected final WebDriverWait wait;
12+
protected final Properties properties;
13+
14+
public BasePage(WebDriver driver, WebDriverWait wait, Properties properties) {
15+
this.driver = driver;
16+
this.wait = wait;
17+
this.properties = properties;
18+
}
19+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.crossover.pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.ElementNotVisibleException;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
10+
import java.util.Properties;
11+
12+
public class GmailPage extends BasePage{
13+
14+
public GmailPage(WebDriver driver, WebDriverWait wait, Properties properties) {
15+
super(driver, wait, properties);
16+
}
17+
18+
public void composeEmail(){
19+
clickCompose();
20+
enterToField();
21+
enterEmailSubject();
22+
enterEmailBody();
23+
markLabelAsSocial();
24+
clickSend();
25+
}
26+
27+
private void clickSend() {
28+
//Click Send
29+
driver.findElement(By.xpath("//*[@role='button' and text()='Send']")).click();
30+
}
31+
32+
private void markLabelAsSocial() {
33+
//Click More settings
34+
wait.until(ExpectedConditions.presenceOfElementLocated(
35+
By.xpath("//div[@class='J-JN-M-I J-J5-Ji Xv L3 T-I-ax7 T-I']/div[2]"))).click();
36+
wait.until(ExpectedConditions.presenceOfElementLocated(
37+
By.xpath("//span[@class='J-Ph-hFsbo']"))).click();
38+
39+
//Choose Social label
40+
wait.until(ExpectedConditions.presenceOfElementLocated(
41+
By.xpath("//div[@class='J-LC-Jz' and text()='Social']/div[@class='J-LC-Jo J-J5-Ji']"))).click();
42+
wait.until(ExpectedConditions.presenceOfElementLocated(
43+
By.xpath("//div[@class='J-JK-Jz' and text()='Apply']"))).click();
44+
}
45+
46+
private void enterEmailBody() {
47+
//Enter email body
48+
String emailBody = properties.getProperty("email.body");
49+
driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).clear();
50+
driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).sendKeys(emailBody);
51+
}
52+
53+
private void enterEmailSubject() {
54+
// emailSubject and emailbody to be used in this unit test.
55+
String emailSubject = properties.getProperty("email.subject");
56+
//Enter subject
57+
By subjectBoxIdentifier = By.name("subjectbox");
58+
wait.until(ExpectedConditions.presenceOfElementLocated(subjectBoxIdentifier));
59+
WebElement subjectBox = driver.findElement(subjectBoxIdentifier);
60+
subjectBox.clear();
61+
subjectBox.sendKeys(emailSubject);
62+
}
63+
64+
private void enterToField() {
65+
By toFieldIdentifier = By.name("to");
66+
wait.until(ExpectedConditions.presenceOfElementLocated(toFieldIdentifier));
67+
WebElement txtBoxToField = driver.findElement(toFieldIdentifier);
68+
txtBoxToField.clear();
69+
String toUserValue = properties.getProperty("username");
70+
txtBoxToField.sendKeys(String.format("%s@gmail.com",toUserValue));
71+
}
72+
73+
private void clickCompose() {
74+
//Click Compose
75+
By composeElementIdentifier = By.xpath("//*[@role='button' and (.)='Compose']");
76+
wait.until(ExpectedConditions.presenceOfElementLocated(composeElementIdentifier));
77+
driver.findElement(composeElementIdentifier).click();
78+
}
79+
80+
public void clickSocialTab() {
81+
//Click Social Tab
82+
wait.until(ExpectedConditions.presenceOfElementLocated(
83+
By.xpath("//div[@class='aKz' and text()='Social']"))).click();
84+
85+
wait.until(ExpectedConditions.presenceOfElementLocated(
86+
By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")));
87+
88+
try{
89+
driver.findElement(By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")).click();
90+
}catch (ElementNotVisibleException e){
91+
driver.findElements(By.xpath("//table[@class='F cf zt']/tbody/tr[1]/td[3]/span")).get(1).click();
92+
}
93+
}
94+
95+
public void openReceivedEmail() {
96+
//Open the received email
97+
try {
98+
driver.findElement(By.xpath("//table[@class='F cf zt']/tbody/tr[1]")).click();
99+
}catch (ElementNotVisibleException e){
100+
driver.findElements(By.xpath("//table[@class='F cf zt']/tbody/tr[1]")).get(1).click();
101+
}
102+
103+
}
104+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.crossover.pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.ExpectedConditions;
7+
import org.openqa.selenium.support.ui.WebDriverWait;
8+
9+
import java.util.Properties;
10+
11+
public class LoginPage extends BasePage{
12+
13+
public LoginPage(WebDriver driver, WebDriverWait wait, Properties properties) {
14+
super(driver, wait, properties);
15+
}
16+
17+
public void login() {
18+
//Enter User Name read from the properties file
19+
WebElement userElement = driver.findElement(By.id("identifierId"));
20+
String userName = properties.getProperty("username");
21+
userElement.sendKeys(userName);
22+
23+
//Click next
24+
driver.findElement(By.id("identifierNext")).click();
25+
26+
//Enter Password read from the properties file
27+
By passwordElementIdentifier = By.name("password");
28+
wait.until(ExpectedConditions.presenceOfElementLocated(passwordElementIdentifier));
29+
WebElement passwordElement = driver.findElement(passwordElementIdentifier);
30+
String passwordValue = properties.getProperty("password");
31+
passwordElement.sendKeys(passwordValue);
32+
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("passwordNext")));
33+
driver.findElement(By.id("passwordNext")).click();
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /