Skip to content

Commit

Permalink
CLDR-17248 cldr-apps-webdriver log in with button not survey?email
Browse files Browse the repository at this point in the history
-Avoid dependency on old survey?email... api, simulate click on Log In button instead

-New small class UserCredentials

-New methods loginWithButton, getCredentialsFromNodePort, clickButtonByXpath, inputTextByXpath, getClickableElementByXpath
  • Loading branch information
btangmu committed Dec 20, 2023
1 parent d5e9d8f commit 6eaa0b4
Showing 1 changed file with 106 additions and 33 deletions.
139 changes: 106 additions & 33 deletions src/test/java/org/unicode/cldr/surveydriver/SurveyDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URL;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -491,7 +492,7 @@ private boolean testFastVotingInner(String page, String url) {
* Log into Survey Tool.
*/
public boolean login() {
String url = BASE_URL + getNodeLoginQuery();
final String url = BASE_URL;
SurveyDriverLog.println("Logging in to " + url);
driver.get(url);

Expand All @@ -506,6 +507,9 @@ public boolean login() {
if (!waitUntilLoadingMessageDone(url)) {
return false;
}
if (!loginWithButton(url)) {
return false;
}
/*
* To make sure we're really logged in, find an element with class "glyphicon-user".
*/
Expand All @@ -516,49 +520,123 @@ public boolean login() {
return true;
}

private static class UserCredentials {
final String username;
final String password;

UserCredentials(String username, String password) {
this.username = username;
this.password = password;
}
}

private boolean loginWithButton(String url) {
final String loginXpath = "//span[text()='Log In']";
final String usernameXpath = "//input[@placeholder='Username']";
final String passwordXpath = "//input[@placeholder='Password']";
final UserCredentials cred = getCredentialsFromNodePort(nodePort);
if (!clickButtonByXpath(loginXpath, url)) {
return false;
}
if (!inputTextByXpath(usernameXpath, cred.username, url)) {
return false;
}
if (!inputTextByXpath(passwordXpath, cred.password, url)) {
return false;
}
return clickButtonByXpath(loginXpath, url);
}

/**
* Get a query string for logging in as a particular user. It may depend on which Selenium node
* we're running on. It could also depend on BASE_URL if we're running on SmokeTest rather than
* localhost.
* Get credentials for logging in as a particular user depending on which Selenium node
* we're running on.
* <p>
* Currently, this set of users depends on running a mysql script on localhost or SmokeTest.
* See scripts/cldr-add-webdrivers.sql, usage "mysql cldrdb < cldr-apps-webdriver/scripts/cldr-add-webdrivers.sql".
* The usernames and passwords here need to agree with that script.
* <p>
* Make sure users have permission to vote in their locales. TC users can vote in all locales,
* so an easy way is to make them all TC.
* Make sure users have permission to vote in their locales.
* <p>
* The range of port numbers 5555, ..., here needs to match selenium-grid-start.sh
*/
private String getNodeLoginQuery() {
private UserCredentials getCredentialsFromNodePort(int nodePort) {
if (nodePort == 5555) {
return "[email protected]&uid=ME0BtTx7J";
}
if (nodePort == 5556) {
return "[email protected]&uid=OjATx0fTt";
}
if (nodePort == 5557) {
return "[email protected]&uid=QEuNcNCvi";
return new UserCredentials("[email protected]", "ME0BtTx7J");
} else if (nodePort == 5556) {
return new UserCredentials("[email protected]", "OjATx0fTt");
} else if (nodePort == 5557) {
return new UserCredentials("[email protected]", "QEuNcNCvi");
} else if (nodePort == 5558) {
return new UserCredentials("[email protected]", "MjpHbYuJY");
} else if (nodePort == 5559) {
return new UserCredentials("[email protected]", "cMkLuCab1");
} else if (nodePort == 5560) {
return new UserCredentials("[email protected]", "qSR.KZ57V");
} else if (nodePort == 5561) {
return new UserCredentials("[email protected]", "r3Lim3OFL");
} else if (nodePort == 5562) {
return new UserCredentials("[email protected]", "LenA3VJSK");
} else if (nodePort == 5563) {
return new UserCredentials("[email protected]%20of%20pakistan%20-%20national%20language%20authority.example.com", "S5fpuRqHW");
} else {
throw new RuntimeException("Unexpected node port " + nodePort);
}
if (nodePort == 5558) {
return "survey?email=wednesdaydriver.kesjczv8q@8sye.afghan-csa.example.com&uid=MjpHbYuJY";
}

private boolean clickButtonByXpath(String xpath, String url) {
WebElement el = getClickableElementByXpath(xpath, url);
if (el == null) {
return false;
}
if (nodePort == 5559) {
return "[email protected]&uid=cMkLuCab1";
try {
el.click();
} catch (Exception e) {
SurveyDriverLog.println("Exception caught while trying to click " + xpath + " element");
SurveyDriverLog.println(e);
return false;
}
if (nodePort == 5560) {
return "[email protected]&uid=qSR.KZ57V";
return true;
}

private boolean inputTextByXpath(String xpath, String text, String url) {
WebElement el = getClickableElementByXpath(xpath, url);
if (el == null) {
return false;
}
if (nodePort == 5561) {
return "[email protected]&uid=r3Lim3OFL";
try {
el.clear();
el.click();
el.sendKeys(text);
el.sendKeys(Keys.RETURN);
} catch (Exception e) {
SurveyDriverLog.println("Exception caught while trying to input text " + text);
SurveyDriverLog.println(e);
return false;
}
if (nodePort == 5562) {
return "[email protected]&uid=LenA3VJSK";
return true;
}

private WebElement getClickableElementByXpath(String xpath, String url) {
try {
wait.until(
(ExpectedCondition<Boolean>) webDriver -> driver.findElement(By.xpath(xpath)) != null
);
} catch (Exception e) {
SurveyDriverLog.println(e);
SurveyDriverLog.println("❌ Test failed, timed out waiting for element to be found by xpath " + xpath + " in url " + url);
return null;
}
if (nodePort == 5563) {
return "[email protected]%20of%20pakistan%20-%20national%20language%20authority.example.com&uid=S5fpuRqHW";
WebElement el;
try {
el = driver.findElement(By.xpath(xpath));
wait.until(ExpectedConditions.elementToBeClickable(el));
} catch (Exception e) {
SurveyDriverLog.println(e);
SurveyDriverLog.println(
"❌ Test failed, timed out waiting for " + xpath + " button to be clickable in " + url);
return null;
}
throw new RuntimeException("Unexpected node port " + nodePort);
return el;
}

/**
Expand Down Expand Up @@ -737,12 +815,7 @@ private int countLogEntriesContainingString(String searchString) {
public boolean waitForTitle(String s, String url) {
try {
wait.until(
new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver webDriver) {
return (webDriver.getTitle().contains(s));
}
}
(ExpectedCondition<Boolean>) webDriver -> (Objects.requireNonNull(webDriver).getTitle().contains(s))
);
} catch (Exception e) {
SurveyDriverLog.println(e);
Expand Down

0 comments on commit 6eaa0b4

Please sign in to comment.