Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. public class StackExample {
  2.  
  3. protected String baseURL;
  4. public String browser;
  5. private static ThreadLocal<WebDriver> threadedDriver = new ThreadLocal<WebDriver>();
  6.  
  7. @BeforeMethod(alwaysRun = true)
  8. @Parameters({ "browser", "loginType" })
  9. public void setup(String browser, String loginType, Method caller)
  10. throws MalformedURLException, InterruptedException {
  11. WebDriver driver = null;
  12.  
  13. // Browsers
  14. if (browser.equalsIgnoreCase("Internet Explorer")) {
  15. System.setProperty("webdriver.ie.driver", "C:\Users\automation\Selenium\IEDriverServer.exe");
  16. driver = new InternetExplorerDriver();
  17. } else if (browser.equalsIgnoreCase("Firefox")) {
  18. System.setProperty("webdriver.gecko.driver", "C:\Users\automation\Selenium\geckodriver.exe");
  19. ProfilesIni firProfiles = new ProfilesIni();
  20. FirefoxProfile wbdrverprofile = firProfiles.getProfile("Webdriver2");
  21. driver = new FirefoxDriver(wbdrverprofile);
  22. } else if (browser.equalsIgnoreCase("chrome")) {
  23. System.setProperty("webdriver.chrome.driver", "C:\Users\automation\Selenium\chromedriver.exe");
  24. driver = new ChromeDriver();
  25. } else if (browser.equalsIgnoreCase("MicrosoftEdge")) {
  26. System.setProperty("webdriver.edge.driver", "C:\Users\automation\Selenium\MicrosoftWebDriver.exe");
  27. driver = new EdgeDriver();
  28. }
  29.  
  30. setWebDriver(driver);
  31. this.browser = browser;
  32. System.out.println(browser);
  33. // initialize(loginType);
  34. System.out.println(loginType);
  35.  
  36. if (loginType.equalsIgnoreCase("client"))
  37. ClientReportFactory
  38. .getTest(StringUtils.join("Client Test"), ' ') + " ("
  39. + browser + ")", "This test is located in class: " + getClass().getName());
  40. else if (loginType.equalsIgnoreCase("advisor"))
  41. AdvisorReportFactory
  42. .getTest(StringUtils.join("Advisor Test"), ' ') + " ("
  43. + browser + ")", "This test is located in class: " + getClass().getName());
  44.  
  45. if (loginType.equalsIgnoreCase("client"))
  46. baseURL = "ClientWebsite.example";
  47. else if (loginType.equalsIgnoreCase("advisor"))
  48. baseURL = "AdvisorWebsite.example";
  49. else {
  50. System.out.println("Client or Advisor must be specified in TestNG XML");
  51. }
  52. driver.get(baseURL);
  53. driver.manage().window().maximize();
  54.  
  55. }
  56.  
  57. // public void initialize(String loginType) throws InterruptedException {
  58. // if (loginType.equalsIgnoreCase("client"))
  59. // baseURL = "ClientWebsite.example";
  60. // else if (loginType.equalsIgnoreCase("advisor"))
  61. // baseURL = "AdvisorWebsite.example";
  62. // else{
  63. // System.out.println("Client or Advisor must be specified in TestNG XML");
  64. // }
  65. // driver.get(baseURL);
  66. // driver.manage().window().maximize();
  67. //
  68. // }
  69.  
  70. public static WebDriver getDriver() {
  71. return threadedDriver.get();
  72. }
  73.  
  74. static void setWebDriver(WebDriver driver) {
  75. threadedDriver.set(driver);
  76. }
  77.  
  78. @AfterMethod // (alwaysRun = true)
  79. @Parameters({ "loginType" })
  80. public void afterMethod(Method caller, String loginType) {
  81. // Here we are making sure we close the same test we opened.
  82. System.out.println(loginType);
  83. if (loginType.equalsIgnoreCase("client"))
  84. ClientReportFactory
  85. .closeTest(StringUtils.join("Client Test"), ' ') + " ("
  86. + browser + ")");
  87. else if (loginType.equalsIgnoreCase("advisor"))
  88. AdvisorReportFactory
  89. .closeTest(StringUtils.join("Advisor Test"), ' ') + " ("
  90. + browser + ")");
  91. getDriver().quit();
  92. threadedDriver.set(null);
  93. }
  94.  
  95. @AfterSuite
  96. @Parameters({ "loginType" })
  97. public void afterSuite(String loginType) {
  98.  
  99. if (loginType.equalsIgnoreCase("client"))
  100. ClientReportFactory.closeReport();
  101. else if (loginType.equalsIgnoreCase("advisor"))
  102. AdvisorReportFactory.closeReport();
  103.  
  104. if (getDriver() != null) {
  105. getDriver().quit();
  106. } else {
  107. System.out.println("Drivers already closed");
  108. }
  109. }
  110. }
  111.  
  112. public class StackExampleTests extends StackExample {
  113.  
  114. @Test(enabled = true, priority = 0)
  115. public void ClientTest1() throws Exception {
  116. ExtentTest t = ClientReportFactory.getTest();
  117. t.log(LogStatus.INFO, "Client 1 Login for " + browser);
  118. try {
  119. Login objLogin = new Login(getDriver());
  120. String username = "username1";
  121. String password = "password1";
  122. t.log(LogStatus.INFO, "Logging in as user: " + username);
  123. objLogin.SignIn(username, password);
  124.  
  125. // perform First Client's tests here
  126.  
  127. } catch (Exception e) {
  128. t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400)
  129. + t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver())));
  130. }
  131. }
  132.  
  133. @Test(enabled = true, priority = 1)
  134. public void ClientTest2 throws Exception{
  135. ExtentTest t = ClientReportFactory.getTest();
  136. t.log(LogStatus.INFO, "Client 2 Login for " + browser);
  137. try {
  138. Login objLogin = new Login(getDriver());
  139. String username = "username2";
  140. String password = "password2";
  141. t.log(LogStatus.INFO, "Logging in as user: " + username);
  142. objLogin.SignIn(username, password);
  143.  
  144. // perform Second Client's tests here
  145.  
  146. } catch (Exception e) {
  147. t.log(LogStatus.WARNING, "Exception found: " + e.getMessage().substring(0, 400)
  148. + t.addBase64ScreenShot(ClientReportFactory.CaptureScreen(getDriver())));
  149. }
  150. }
  151. }
  152.  
  153. Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 44694
  154. Only local connections are allowed.
  155.  
  156. Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 12513
  157. Only local connections are allowed.
  158.  
  159. Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 32651
  160. Only local connections are allowed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement