Guest User

Untitled

a guest
Apr 17th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. package pl.org.opi.jsa.ui.pages;
  2.  
  3. import org.openqa.selenium.*;
  4. import org.openqa.selenium.support.ui.ExpectedConditions;
  5. import org.openqa.selenium.support.ui.FluentWait;
  6. import pl.org.opi.jsa.ui.Config.ConfigProfiles;
  7. import pl.org.opi.jsa.ui.pages.commons.DialogWindow;
  8. import pl.org.opi.jsa.ui.user.User;
  9. import pl.org.opi.jsa.ui.excepctions.AuthorizationException;
  10. import pl.org.opi.utils.selenium.*;
  11.  
  12. import java.io.File;
  13. import java.time.Duration;
  14. import java.util.concurrent.TimeUnit;
  15. import java.util.concurrent.atomic.AtomicReference;
  16.  
  17. @SuppressWarnings("unchecked")
  18. public class MainPage extends Page {
  19.  
  20.  
  21. static final By BROKER_ALERT_MESSAGE = By.xpath("//*[@test-broker-message]");
  22. static final By CLOSE_BROKER_ALERT_BAR = By.xpath("//*[@test-close-broker-bar]");
  23. static final By CLOSE_COOKIE_BAR = By.xpath("//*[@test-close-cookie-bar]");
  24. static final By LOGGED_USER = By.xpath("//*[@test-login-user]");
  25. static final By LOG_OUT_BUTTON = By.xpath("//*[@test-logout-btn]");
  26.  
  27. private static final By POLON_LOGIN_BUTTON = By.xpath("//*[@test-polon-login-btn]");
  28. private static final By LOGIN_INPUT = By.xpath("//*[@test-login-input]");
  29. private static final By PASSWORD_INPUT = By.xpath("//*[@test-password-input]");
  30. private static final By LOGIN_BUTTON = By.xpath("//*[@test-login-button]");
  31. private static final By LOGIN_CONTEXT = By.xpath("//*[@test-choice-context]");
  32. private static final String LOGIN_CONTEXT_VALUE = "//*[@test-choice-context-value]//span[contains(text(),'%s')]";
  33. private static final By LOGIN_CONTEXT_SUBMIT_BUTTON = By.xpath("//*[@test-choice-context-btn]");
  34.  
  35. private static final By ACCEPT_REGULATION_BUTTON = By.xpath("//*[@test-accept-regulation-btn]");
  36.  
  37. private static final By CONTEXT_LIST = By.xpath("//*[@test-context-list]");
  38. private static final By CURRENT_CONTEXT = By.xpath("//*[@test-context-value]");
  39.  
  40. private static final By LOGIN_ERROR_MESSAGE = By.xpath("//*[@test-login-error]");
  41.  
  42. private By PERSON = By.xpath("//*[@test-choice-person]");
  43. private static final String PERSON_VALUE = "//*[@test-choice-person-value]//span[contains(text(),'%s')]";
  44. //////////TO DO !!!!!!!!!!!!!////////////////////////////////
  45. static final By JSA_LOGO = By.xpath("//img[@alt='Logo - Jednolity System Antyplagiatowy']");
  46. static final By HOME_BUTTON = By.xpath("//*[@class='jsa__navbar__menu']//a[@href='/home']");
  47. static final By NEWS_BUTTON = By.xpath("//div[@class='jsa__navbar__menu']//a[@href='/news']");
  48. static final By HELP_CENTER_BUTTON = By.xpath("//div[@id='generalnavbar']//a[@href='/help-center']");
  49. static final By HELP_CENTER_FAQ = By.xpath("");
  50. static final By HELP_CENTER_KNOWLEDGE = By.xpath("");
  51. static final By HELP_CENTER_HEPL = By.xpath("");
  52.  
  53. static final By USER_PANEL_MENU = By.xpath("//*[@test-user-panel-menu-btn]");
  54. static final By USER_PANEL_USERS = By.xpath("//*[@test-users-panel]");
  55. static final By USER_PANEL_PERMISSIONS = By.xpath("//*[@test-users-permissions]");
  56. static final By USER_PANEL_CONTRACTS = By.xpath("//*[@test-users-contracts]");
  57. static final By USER_PANEL_REPORTS = By.xpath("//*[@test-users-reports]");
  58. static final By USER_PANEL_SETTINGS = By.xpath("//*[@test-users-settings]");
  59. static final By USER_PANEL_PROFILE = By.xpath("//*[@test-users-profiles]");
  60.  
  61.  
  62. //STRONA USTAWIENIA HASŁA PO PO RAZ PIERWSZY
  63.  
  64. static final By SAVE_PASSWORD_BUTTON = By.xpath("//*[@test-save-button]");
  65. static final By NEW_PASSWORD = By.xpath("//*[@test-password]");
  66. static final By REPEAT_NEW_PASSWORD = By.xpath("//*[@test-repeat-password]");
  67. static final By NEW_USERNAME = By.xpath("//*[@test-username]");
  68.  
  69.  
  70. //STRONA PRZYPOMNIJ HASŁO
  71.  
  72. static final By REMINDER_PASSWORD = By.xpath("//*[@test-remind-password]");
  73. static final By EMAIL_INPUT_REMINDER_PASSWORD = By.xpath("//*[@test-remind-password]");
  74.  
  75.  
  76.  
  77. static final By SPINNER = By.xpath("//*[@test-spinner]");
  78.  
  79. MainPage(TestProfile testProfile, Browser browser, AtomicReference<Page> reference) {
  80. super(ConfigProfiles.getString("test.url"), testProfile, browser, reference);
  81. }
  82.  
  83. MainPage(WebDriver webDriver, TestProfile testProfile, Browser browser, AtomicReference<Page> reference) {
  84. super(webDriver, testProfile, browser, reference);
  85. }
  86.  
  87. public static MainPage newInstance(TestProfile testProfile, Browser browser, AtomicReference<Page> reference) {
  88.  
  89. return new MainPage(testProfile, browser, reference);
  90. }
  91.  
  92. public static MainPage instance(WebDriver webDriver, TestProfile testProfile, Browser browser, AtomicReference<Page> reference) {
  93. MainPage instance = SeleniumObject.get(webDriver, MainPage.class);
  94. return instance != null ? instance : new MainPage(webDriver, testProfile, browser, reference);
  95.  
  96. }
  97.  
  98. protected void waitForPageLoad() {
  99. sleep(1000);
  100. wait.forJS();
  101. }
  102.  
  103. public <T extends MainPage> T closeCookiebar() {
  104. perform.click(CLOSE_COOKIE_BAR);
  105. wait.forInvisibility(CLOSE_COOKIE_BAR);
  106. return (T) this;
  107. }
  108.  
  109. public ContractsPage login(User user) {
  110. fillLoginAndPassword(user);
  111. clickLoginButton();
  112. selectContext(user);
  113.  
  114. return ContractsPage.instance(webDriver, testProfile, browser, reference);
  115. }
  116.  
  117. public ContractsPage loginPolon(User user) {
  118. perform.click(POLON_LOGIN_BUTTON);
  119. fillLoginAndPassword(user);
  120. clickLoginButton();
  121.  
  122. return ContractsPage.instance(webDriver, testProfile, browser, reference);
  123. }
  124.  
  125. public ContractsPage loginHD(User userHD, String person) {
  126. fillLoginAndPassword(userHD);
  127. clickLoginButton();
  128. selectContext(userHD);
  129.  
  130. perform.clearAndSendKeys(PERSON, person);
  131. perform.click(By.xpath(String.format(PERSON_VALUE, person)));
  132. clickLoginContextButton();
  133.  
  134. return ContractsPage.instance(webDriver, testProfile, browser, reference);
  135. }
  136.  
  137. private void fillLoginAndPassword(User user) {
  138. waitForPageLoad();
  139. perform.clearAndSendKeys(LOGIN_INPUT, user.getLogin());
  140. perform.clearAndSendKeys(PASSWORD_INPUT, user.getPassword());
  141. }
  142.  
  143. private void clickLoginButton() {
  144. perform.click(LOGIN_BUTTON);
  145. waitForPageLoad();
  146. }
  147.  
  148. private void clickLoginContextButton() {
  149. perform.click(LOGIN_CONTEXT_SUBMIT_BUTTON);
  150. waitForPageLoad();
  151. }
  152.  
  153. private void selectContext(User user) {
  154. waitForPageLoad();
  155. if (is.present(LOGIN_CONTEXT_SUBMIT_BUTTON)) {
  156. perform.click(LOGIN_CONTEXT);
  157. perform.click(By.xpath(String.format(LOGIN_CONTEXT_VALUE, user.getContext())));
  158. perform.click(LOGIN_CONTEXT_SUBMIT_BUTTON);
  159. waitForPageLoad();
  160. }
  161. acceptRegulation();
  162. }
  163.  
  164. private void acceptRegulation() {
  165. if (is.visible(ACCEPT_REGULATION_BUTTON)) {
  166. perform.click(ACCEPT_REGULATION_BUTTON);
  167. }
  168. }
  169.  
  170.  
  171. public boolean isLoggedIn() {
  172. wait.forVisibility(LOG_OUT_BUTTON);
  173. boolean isLogged = is.visible(LOGGED_USER);
  174.  
  175. if (isLogged) {
  176. return true;
  177. } else {
  178. throw new AuthorizationException("Only logged in user has access to this page.");
  179. }
  180. }
  181.  
  182. public MainPage logout() {
  183. if (!isLoggedIn()) throw new AuthorizationException("Only logged in user can log out.");
  184. perform.click(LOG_OUT_BUTTON);
  185. return MainPage.instance(webDriver, testProfile, browser, reference);
  186. }
  187.  
  188. public UsersPage goToUsersPage() {
  189. isLoggedIn();
  190. wait.forVisibility(USER_PANEL_USERS);
  191. perform.click(USER_PANEL_USERS);
  192. return UsersPage.instance(webDriver, testProfile, browser, reference);
  193. }
  194.  
  195. public PermissionsPage goToPermissionsPage() {
  196. isLoggedIn();
  197. perform.click(USER_PANEL_PERMISSIONS);
  198. return PermissionsPage.instance(webDriver, testProfile, browser, reference);
  199. }
  200.  
  201. public ContractsPage goToContractsPage() {
  202. isLoggedIn();
  203. perform.click(USER_PANEL_CONTRACTS);
  204. return ContractsPage.instance(webDriver, testProfile, browser, reference);
  205. }
  206.  
  207. public SettingsPage goToSettingsPage() {
  208. isLoggedIn();
  209. perform.click(USER_PANEL_SETTINGS);
  210. return SettingsPage.instance(webDriver, testProfile, browser, reference);
  211. }
  212.  
  213. public ProfilePage goToProfilePage() {
  214. isLoggedIn();
  215. perform.click(USER_PANEL_PROFILE);
  216. return ProfilePage.instance(webDriver, testProfile, browser, reference);
  217. }
  218.  
  219. public ProfilePage goTo() {
  220. // perform.click();
  221. return ProfilePage.instance(webDriver, testProfile, browser, reference);
  222. }
  223.  
  224. void sleep(int millis) {
  225. try {
  226. Thread.sleep(millis);
  227. } catch (InterruptedException e) {
  228. e.printStackTrace();
  229. }
  230. }
  231.  
  232. void waitForSpinner() {
  233.  
  234. sleep(1500);
  235. }
  236.  
  237. public String getDialogMessage() {
  238.  
  239. return new DialogWindow(webDriver).getDialogMessage();
  240. }
  241.  
  242. public String getLoginErrorMessage() {
  243. return get.text(LOGIN_ERROR_MESSAGE);
  244. }
  245.  
  246.  
  247. public <T extends MainPage> T clickDialogOK() {
  248. new DialogWindow(webDriver).clickDialogOK();
  249. waitForPageLoad();
  250. return (T) this;
  251. }
  252.  
  253. public boolean isDialogSuccess() {
  254. return new DialogWindow(webDriver).isDialogSuccess();
  255. }
  256.  
  257. public boolean isDaialogUnsuccess() {
  258. return new DialogWindow(webDriver).isDaialogUnsuccess();
  259. }
  260.  
  261. public void clickBackButtonDialog() {
  262. new DialogWindow(webDriver).clickBackButtonDialog();
  263. }
  264.  
  265. public String getCurrentURL() {
  266. return webDriver.getCurrentUrl();
  267. }
  268.  
  269. public MainPage setPassword(User user) {
  270.  
  271. user.setLogin(get.value(NEW_USERNAME));
  272. System.out.println(get.text(NEW_USERNAME));
  273. perform.clearAndSendKeys(NEW_PASSWORD, user.getPassword());
  274. perform.clearAndSendKeys(REPEAT_NEW_PASSWORD, user.getPassword());
  275. perform.click(SAVE_PASSWORD_BUTTON);
  276.  
  277. new DialogWindow(webDriver).clickDialogOK();
  278.  
  279. return MainPage.instance(webDriver, testProfile, browser, reference);
  280. }
  281.  
  282. public MainPage reminderPassword(String email) {
  283.  
  284. String[] emailName = email.split("@");
  285. String emailMessage = "https://preview.putsbox.com/p/" + emailName[0] + "/last";
  286.  
  287. perform.click(REMINDER_PASSWORD);
  288. perform.clearAndSendKeys(EMAIL_INPUT_REMINDER_PASSWORD,emailName[0]);
  289.  
  290. new DialogWindow(webDriver).clickDialogOK();
  291. return MainPage.instance(webDriver, testProfile, browser, reference);
  292. }
  293.  
  294.  
  295. @Override
  296. protected void beforeQuit() {
  297. if (isLoggedIn()) logout();
  298.  
  299. }
  300.  
  301. public void waitForFile(long timeout, File file) {
  302. wait.forAny(timeout, webDriver -> file.exists());
  303. }
  304. }
Add Comment
Please, Sign In to add comment