Skip to content

Commit c69af0f

Browse files
committed
Refactor a bit
1 parent 9f97850 commit c69af0f

File tree

8 files changed

+106
-27
lines changed

8 files changed

+106
-27
lines changed

.idea/misc.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/java/pages/FriendsPage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
import java.util.List;
88

99
public class FriendsPage extends HelperBase {
10+
11+
public static final By FRIEND_LIST_SELECTOR = By.xpath(".//ul[@class = \"ugrid_cnt\"]//li[@class=\"ugrid_i\"]");
12+
public static final By FRIENDS_NAME_LOCATOR = By.xpath(".//*[@class = \"ugrid_cnt\"]//*[@class=\"ucard-w_t ellip-i\"]");
13+
1014
public FriendsPage(WebDriver driver) {
1115
super(driver);
1216
}
1317

1418
public List<WebElement> getListOfMyFriends() {
15-
return driver.findElements(By.xpath(".//ul[@class = \"ugrid_cnt\"]//li[@class=\"ugrid_i\"]"));
19+
return driver.findElements(FRIEND_LIST_SELECTOR);
1620
}
1721

1822
public List<WebElement> getListOfFriendsName() {
19-
return driver.findElements(By.xpath(".//*[@class = \"ugrid_cnt\"]//*[@class=\"ucard-w_t ellip-i\"]"));
23+
return driver.findElements(FRIENDS_NAME_LOCATOR);
2024
}
2125
}

src/test/java/pages/HelperBase.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package pages;
22

3+
import com.google.common.base.Preconditions;
34
import org.openqa.selenium.By;
45
import org.openqa.selenium.NoSuchElementException;
56
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.support.ui.ExpectedCondition;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
10+
import java.util.concurrent.TimeUnit;
611

712
public abstract class HelperBase {
813
protected WebDriver driver;
@@ -31,4 +36,32 @@ protected boolean isElementPresent(By locator) {
3136
return false;
3237
}
3338
}
39+
40+
public boolean explicitWait(final ExpectedCondition<?> condition, long maxCheckTimeInSeconds, long millisecondsBetweenChecks) {
41+
Preconditions.checkNotNull(condition, "Condition must be not null");
42+
Preconditions.checkArgument(TimeUnit.MINUTES.toSeconds(3) > maxCheckTimeInSeconds, "Max check time in seconds should be less than 3 minutes");
43+
checkConditionTimeouts(maxCheckTimeInSeconds, millisecondsBetweenChecks);
44+
try {
45+
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
46+
WebDriverWait explicitWait = new WebDriverWait(driver, maxCheckTimeInSeconds, millisecondsBetweenChecks);
47+
explicitWait.until(condition);
48+
return true;
49+
} catch (Exception e) {
50+
return false;
51+
} finally {
52+
if (driver != null) {
53+
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
54+
} else {
55+
throw new IllegalArgumentException("Driver shouldnt be null");
56+
}
57+
}
58+
}
59+
60+
private void checkConditionTimeouts(long maxCheckTimeInSeconds, long millisecondsBetweenChecks) {
61+
Preconditions.checkState(maxCheckTimeInSeconds > 0, "maximum check time in seconds must be not 0");
62+
Preconditions.checkState(millisecondsBetweenChecks > 0, "milliseconds count between checks must be not 0");
63+
Preconditions.checkState(millisecondsBetweenChecks < (maxCheckTimeInSeconds * 1000),
64+
"Millis between checks must be less than max seconds to wait");
65+
}
66+
3467
}

src/test/java/pages/LoginPage.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
public class LoginPage extends HelperBase {
88

9+
public static final By FIELD_EMAIL = By.id("field_email");
10+
public static final By FIELD_PASSWORD = By.id("field_password");
11+
public static final By AUTHENTICATION = By.xpath(".//input[contains(@data-l,'sign_in')]");
12+
913
public LoginPage(WebDriver driver) {
1014
super(driver);
1115
}
1216

1317
public void logIn(TestBot bot) {
14-
type(By.id("field_email"), bot.getUsername());
15-
type(By.id("field_password"), bot.getPassword());
16-
click(By.xpath(".//input[contains(@data-l,'sign_in')]"));
18+
type(FIELD_EMAIL, bot.getUsername());
19+
type(FIELD_PASSWORD, bot.getPassword());
20+
click(AUTHENTICATION);
1721
}
1822
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pages;
2+
3+
import org.openqa.selenium.WebDriver;
4+
5+
public class PersonFactory {
6+
private PersonFactory() {}
7+
8+
public static PersonPage getNewPersonFactory(WebDriver driver) {
9+
return new PersonPage(driver);
10+
}
11+
12+
}

src/test/java/pages/PersonPage.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,38 @@
22

33
import org.openqa.selenium.By;
44
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.support.ui.ExpectedConditions;
56

67
public class PersonPage extends HelperBase {
7-
By friendshipButton = By.xpath(".//*[@data-l=\"t,invite\"]");
8+
private static final By FRIENDSHIP_BUTTON = By.xpath(".//*[@data-l=\"t,invite\"]");
9+
private static final By REQUEST_BUTTON = By.xpath(".//div[contains(@class, \"dropdown h-mod\")]");
10+
private static final By REVOKED_FRIENDSHIP = By.xpath(".//a[contains(@href, \"act=REVOKE\")]");
11+
public static final By FREAND_LIST = By.xpath(".//*[contains(@hrefattrs, \"NavMenu_Friend_Friends\")]");
12+
public static final By INVITE_FRIEANDS = By.xpath(".//*[@data-l='t,invite']//*[contains(@class, 'svg-ico_check')]");
813

9-
public PersonPage(WebDriver driver) {
10-
super(driver);
11-
}
14+
public PersonPage(WebDriver driver) { super(driver); }
1215

1316
public void addToFriends() {
14-
click(friendshipButton);
17+
click(FRIENDSHIP_BUTTON);
1518
}
1619

1720
public boolean isInvitedToFriends() {
18-
return isElementPresent(By.xpath(".//*[@data-l='t,invite']//*[contains(@class, 'svg-ico_check')]"));
21+
return isElementPresent(INVITE_FRIEANDS);
1922
}
2023

2124
public void revokeInvite() {
22-
click(friendshipButton);
23-
click(By.xpath(".//a[contains(@href, \"act=REVOKE\")]"));
25+
click(REQUEST_BUTTON);
26+
if(explicitWait(
27+
ExpectedConditions.visibilityOfNestedElementsLocatedBy(
28+
REQUEST_BUTTON,
29+
REVOKED_FRIENDSHIP
30+
),
31+
5,
32+
500))
33+
click(REVOKED_FRIENDSHIP);
2434
}
2535

2636
public void goToFriends() {
27-
click(By.xpath(".//*[contains(@hrefattrs, \"NavMenu_Friend_Friends\")]"));
37+
click(FREAND_LIST);
2838
}
2939
}

src/test/java/pages/UserMainPage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
public class UserMainPage extends HelperBase {
77

8+
public static final By GO_TO_FREAND_PAGE = By.xpath(".//*[@class=\"toolbar_nav\"]/*[@data-l=\"t,friends\"]");
9+
810
public UserMainPage(WebDriver driver) {
911
super(driver);
1012
}
1113

1214
public void goToPageFriends() {
13-
driver.findElement(By.xpath(".//*[@class=\"toolbar_nav\"]/*[@data-l=\"t,friends\"]")).click();
15+
driver.findElement(GO_TO_FREAND_PAGE).click();
1416
}
1517
}

src/test/java/tests/OkTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
import pages.PersonPage;
1111
import pages.UserMainPage;
1212

13+
import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
14+
1315
import java.util.List;
1416

17+
import static pages.PersonFactory.getNewPersonFactory;
18+
1519

1620
public class OkTest extends TestBase {
1721
@BeforeAll
@@ -25,25 +29,32 @@ public void checkFriend() {
2529
bot = BotFactory.getOkBot();
2630
new LoginPage(driver).logIn(bot);
2731
new UserMainPage(driver).goToPageFriends();
28-
List<WebElement> friends = new FriendsPage(driver).getListOfMyFriends();
29-
friends.get(0).click();
30-
new PersonPage(driver).goToFriends();
31-
friends = new FriendsPage(driver).getListOfFriendsName();
32-
friends.get(0).click();
33-
PersonPage friendPage = new PersonPage(driver);
32+
33+
clickOnFirstFriends();
34+
getNewPersonFactory(driver).goToFriends();
35+
clickOnFirstFriends();
36+
37+
PersonPage friendPage = getNewPersonFactory(driver);
3438
friendPage.addToFriends();
3539
Assertions.assertTrue(friendPage.isInvitedToFriends());
3640
}
3741

42+
private void clickOnFirstFriends() {
43+
new FriendsPage(driver).getListOfMyFriends().get(0).click();
44+
45+
// new FriendsPage(driver)
46+
// .getListOfFriendsName()
47+
// .stream()
48+
// .filter() // нужно найти тег по которому отсеим себя и тех кто уже нам друг
49+
// .findFirst()
50+
// .isPresent;
51+
}
52+
3853
@Test
3954
public void revokeFriendship() {
4055
driver.navigate().refresh();
4156
PersonPage currentPage = new PersonPage(driver);
4257
currentPage.revokeInvite();
43-
try {
44-
Thread.sleep(2000);
45-
} catch (InterruptedException ignored) {
46-
}
47-
Assertions.assertFalse(currentPage.isInvitedToFriends());
58+
Assertions.assertTrue(currentPage.isInvitedToFriends());
4859
}
4960
}

0 commit comments

Comments
 (0)