Guest User

Untitled

a guest
Jan 4th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 KB | None | 0 0
  1. package base;
  2.  
  3. import models.ui.Config;
  4. import models.ui.ExportTestData;
  5. import models.ui.TestData;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.openqa.selenium.JavascriptExecutor;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.chrome.ChromeOptions;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;
  12. import org.openqa.selenium.support.PageFactory;
  13. import org.testng.annotations.AfterMethod;
  14. import org.testng.annotations.AfterSuite;
  15. import org.testng.annotations.BeforeMethod;
  16. import org.testng.annotations.BeforeSuite;
  17. import pages.WhatfixLandingPage;
  18. import pages.WhatfixLoginPage;
  19. import util.YamlReader;
  20.  
  21. import java.io.File;
  22. import java.lang.reflect.Method;
  23. import java.net.MalformedURLException;
  24. import java.net.URISyntaxException;
  25. import java.net.URL;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28.  
  29. /**
  30. * Created by chandanjavaregowda on 03/04/17.
  31. */
  32. public abstract class BaseTest {
  33. protected static TestData data;
  34. protected static ExportTestData exportData;
  35. private static Config config;
  36. private static DesiredCapabilities capabilities;
  37. public static WebDriver driver;
  38. private String jenkinsUrl;
  39. private String url;
  40. private String path;
  41. private boolean isLoginLogoutRequired = false;
  42. protected int flowsBeforeAddition = 0;
  43. private String username;
  44. private String password;
  45.  
  46. // set up config and data
  47. static {
  48. config = new YamlReader("config.yml").readConfig();
  49. SetIpAddress ipAddress = new SetIpAddress();
  50. data = ipAddress.setData();
  51. exportData = new YamlReader("exportData.yml").readExportTestData();
  52. }
  53.  
  54. @BeforeSuite
  55. public void setUp() {
  56. try {
  57. setBrowser();
  58. if (config.isEnableWhatfixPlugin()) {
  59. enableWhatfixExtension();
  60. }
  61. driver = new RemoteWebDriver(new URL(config.getNodeUrl()), capabilities);
  62. if (config.isBringToFront()) {
  63. bringToForeground();
  64. }
  65. if (config.isMaximize()) {
  66. maximize();
  67. }
  68. } catch (MalformedURLException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. // default constructor, it doesn't navigate to whatfix url and login
  74. public BaseTest() {
  75. }
  76.  
  77. // initializes the url and other params
  78. private void init(String path) {
  79. this.path = path;
  80. this.url = data.getWhatfixUrl() + path;
  81. this.jenkinsUrl = data.getWhatfixUrl() + "automation3";
  82. this.isLoginLogoutRequired = true;
  83. }
  84.  
  85. // constructor which will support navigating to whatfix url with {path} param
  86. public BaseTest(String path) {
  87. init(path);
  88. }
  89.  
  90. // constructor which will support navigating to whatfix url with {path} param and passed username and password
  91. public BaseTest(String path, String username, String password) {
  92. this.username = username;
  93. this.password = password;
  94. init(path);
  95. }
  96.  
  97. // logs in to the app
  98. @BeforeMethod
  99. public void handleTestMethodName(Method method) {
  100. System.out.println("--------------------------------------------------------------------------------------");
  101. System.out.println("--------------------------------------------------------------------------------------");
  102. System.out.println("Starting test: " + getClass().getSimpleName() + " --> " + method.getName());
  103. System.out.println("--------------------------------------------------------------------------------------");
  104. System.out.println("--------------------------------------------------------------------------------------");
  105.  
  106. if (isLoginLogoutRequired) {
  107. navigateToWhatfixUrl();
  108. login();
  109. }
  110. }
  111.  
  112. protected void navigateToWhatfixUrl() {
  113. navigateTo(url, true);
  114. }
  115.  
  116. protected void navigateTo(String url) {
  117. navigateTo(url, false);
  118. }
  119.  
  120. protected void navigateTo(String url, boolean isWhatfixUrl) {
  121. String navigationUrl = null;
  122.  
  123. if (isWhatfixUrl) {
  124. if (StringUtils.isEmpty(System.getProperty("env"))) {
  125. navigationUrl = url;
  126. } else if (StringUtils.equalsIgnoreCase(System.getProperty("env"), "jenkins")) {
  127. navigationUrl = jenkinsUrl;
  128. }
  129. } else {
  130. navigationUrl = url;
  131. }
  132.  
  133. System.out.println("Navigating to URL --> " + navigationUrl);
  134. driver.get(navigationUrl);
  135. }
  136.  
  137. protected void login() {
  138. if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
  139. if (path.equals("automation1")) {
  140. flowsBeforeAddition = loginAndGetFlowCount(data.getUsername(), data.getPassword());
  141. } else if (path.equals("automation2")) {
  142. flowsBeforeAddition = loginAndGetFlowCount(data.getmyUsername(), data.getmyPassword());
  143. } else if (path.equals("whatfix-uat-2")) {
  144. flowsBeforeAddition = loginAndGetFlowCount(data.getUsername(), data.getPassword());
  145. }
  146. } else {
  147. flowsBeforeAddition = loginAndGetFlowCount(username, password);
  148. }
  149. }
  150.  
  151. private int loginAndGetFlowCount(String username, String password) {
  152. WhatfixLoginPage whatfixLoginPage = PageFactory.initElements(driver, WhatfixLoginPage.class);
  153. return whatfixLoginPage
  154. .enterUsername(username)
  155. .enterPassword(password)
  156. .submit()
  157. .getFlowCount();
  158. }
  159.  
  160. protected void launchWhatfixPlugin() {
  161. JavascriptExecutor js = (JavascriptExecutor) driver;
  162. String sc = "var script = document.createElement('script');\n" +
  163. "script.type = 'text/javascript';\n" +
  164. "script.src = '//whatfix.com/gluexsi/gluexsi.nocache.js';\n" +
  165. "document.head.appendChild(script);";
  166. js.executeScript(sc);
  167. }
  168.  
  169. private BaseTest bringToForeground() {
  170. String currentWindowHandle = driver.getWindowHandle();
  171. ((JavascriptExecutor) driver).executeScript("alert('Test')");
  172. driver.switchTo().alert().accept();
  173. driver.switchTo().window(currentWindowHandle);
  174. return this;
  175. }
  176.  
  177. private void maximize() {
  178. System.out.println("Maximizing browser...");
  179. driver.manage().window().maximize();
  180. }
  181.  
  182. private BaseTest enableWhatfixExtension() {
  183. ChromeOptions options = new ChromeOptions();
  184. try {
  185. options.addExtensions(new File(this.getClass().getClassLoader().getResource("whatfix_v4.1.crx").toURI()));
  186. Map<String, Object> prefs = new HashMap<>();
  187. prefs.put("credentials_enable_service", false);
  188. prefs.put("password_manager_enabled", false);
  189. options.setExperimentalOption("prefs", prefs);
  190. } catch (URISyntaxException e) {
  191. e.printStackTrace();
  192. }
  193. capabilities.setCapability(ChromeOptions.CAPABILITY, options);
  194. return this;
  195. }
  196.  
  197. private BaseTest setBrowser() {
  198. System.out.println("Launching " + config.getBrowser().toUpperCase() + " browser.");
  199. if (config.getBrowser().equals("chrome")) {
  200. capabilities = DesiredCapabilities.chrome();
  201. }
  202. return this;
  203. }
  204.  
  205. @AfterMethod
  206. public void logout() {
  207. System.out.println("Starting after method...");
  208. if (isLoginLogoutRequired) {
  209. WhatfixLandingPage whatfixLandingPage = PageFactory.initElements(driver, WhatfixLandingPage.class);
  210. System.out.println("Logging out...");
  211. navigateTo(url);
  212. whatfixLandingPage
  213. .settings()
  214. .logout();
  215. }
  216.  
  217. System.out.println("");
  218. System.out.println("");
  219. System.out.println("");
  220. }
  221.  
  222. @AfterSuite
  223. public void tearDown() {
  224. System.out.println("Starting after suite...");
  225. driver.quit();
  226. }
  227. }
Add Comment
Please, Sign In to add comment