From 6eaa0b46845230cb3011597e0ff7a34ee4f0a3c6 Mon Sep 17 00:00:00 2001 From: btangmu Date: Wed, 20 Dec 2023 14:12:20 -0500 Subject: [PATCH] CLDR-17248 cldr-apps-webdriver log in with button not survey?email -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 --- .../cldr/surveydriver/SurveyDriver.java | 139 +++++++++++++----- 1 file changed, 106 insertions(+), 33 deletions(-) diff --git a/src/test/java/org/unicode/cldr/surveydriver/SurveyDriver.java b/src/test/java/org/unicode/cldr/surveydriver/SurveyDriver.java index a9a6816..eaa5225 100644 --- a/src/test/java/org/unicode/cldr/surveydriver/SurveyDriver.java +++ b/src/test/java/org/unicode/cldr/surveydriver/SurveyDriver.java @@ -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; @@ -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); @@ -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". */ @@ -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. *

* 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. *

- * 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. *

* 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 "survey?email=sundaydriver.ta9emn2f.@czca.bangladesh.example.com&uid=ME0BtTx7J"; - } - if (nodePort == 5556) { - return "survey?email=mondaydriver.fvuisg2in@sisi.sil.example.com&uid=OjATx0fTt"; - } - if (nodePort == 5557) { - return "survey?email=tuesdaydriver.smw4grsg0@ork0.netflix.example.com&uid=QEuNcNCvi"; + return new UserCredentials("sundaydriver.ta9emn2f.@czca.bangladesh.example.com", "ME0BtTx7J"); + } else if (nodePort == 5556) { + return new UserCredentials("mondaydriver.fvuisg2in@sisi.sil.example.com", "OjATx0fTt"); + } else if (nodePort == 5557) { + return new UserCredentials("tuesdaydriver.smw4grsg0@ork0.netflix.example.com", "QEuNcNCvi"); + } else if (nodePort == 5558) { + return new UserCredentials("wednesdaydriver.kesjczv8q@8sye.afghan-csa.example.com", "MjpHbYuJY"); + } else if (nodePort == 5559) { + return new UserCredentials("thursdaydriver.klxizrpyc@p9mn.welsh-lc.example.com", "cMkLuCab1"); + } else if (nodePort == 5560) { + return new UserCredentials("fridaydriver.kclabyoxi@fgkg.mozilla.example.com", "qSR.KZ57V"); + } else if (nodePort == 5561) { + return new UserCredentials("saturdaydriver.oelbvfn0x@smiz.cherokee.example.com", "r3Lim3OFL"); + } else if (nodePort == 5562) { + return new UserCredentials("backseatdriver.cogihy42h@jqs9.india.example.com", "LenA3VJSK"); + } else if (nodePort == 5563) { + return new UserCredentials("studentdriver.h.ze76.2p@nd3e.government%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 "survey?email=thursdaydriver.klxizrpyc@p9mn.welsh-lc.example.com&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 "survey?email=fridaydriver.kclabyoxi@fgkg.mozilla.example.com&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 "survey?email=saturdaydriver.oelbvfn0x@smiz.cherokee.example.com&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 "survey?email=backseatdriver.cogihy42h@jqs9.india.example.com&uid=LenA3VJSK"; + return true; + } + + private WebElement getClickableElementByXpath(String xpath, String url) { + try { + wait.until( + (ExpectedCondition) 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 "survey?email=studentdriver.h.ze76.2p@nd3e.government%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; } /** @@ -737,12 +815,7 @@ private int countLogEntriesContainingString(String searchString) { public boolean waitForTitle(String s, String url) { try { wait.until( - new ExpectedCondition() { - @Override - public Boolean apply(WebDriver webDriver) { - return (webDriver.getTitle().contains(s)); - } - } + (ExpectedCondition) webDriver -> (Objects.requireNonNull(webDriver).getTitle().contains(s)) ); } catch (Exception e) { SurveyDriverLog.println(e);