Guest User

Untitled

a guest
Sep 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. static {
  2. URL = "https://website.com";
  3. DEFAULT_WAIT_TIME = 60;
  4. SYSTEM_ID = "IDnumber";
  5. USERNAME = "Username1";
  6. PASSWORD = "Password1";
  7. }
  8.  
  9. import static org.junit.Assert.fail;
  10.  
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. import org.junit.After;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16. import org.openqa.selenium.*;
  17. import org.openqa.selenium.firefox.FirefoxDriver;
  18. import org.openqa.selenium.support.ui.ExpectedConditions;
  19. import org.openqa.selenium.support.ui.WebDriverWait;
  20.  
  21. public class Login {
  22. private WebDriver driver;
  23. private String baseURL;
  24. private StringBuffer verificationErrors = new StringBuffer();
  25.  
  26. @Before
  27. public void setUp() throws Exception {
  28. //driver = new FirefoxDriver();
  29. //baseURL = Environment.URL;
  30. driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
  31. }
  32.  
  33. public static void doLogin(WebDriver driver, String baseURL) {
  34. WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
  35. System.out.println("Log in to trunk...");
  36. driver.get(baseURL);
  37.  
  38. wait.until(ExpectedConditions.presenceOfElementLocated(By.id("company_id")));
  39. if (driver.findElement(By.xpath("//div[span='Log In']/span")).isEnabled()) {
  40. System.out.println(" 1 - 'Login' page is opened");
  41. } else {
  42. System.out.println(" !ERROR! - Can NOT open 'Login' page");
  43. driver.findElement(By.id("This is to throw exception if Can NOT open 'Login' page"));
  44. }
  45. driver.findElement(By.id("company_id")).clear();
  46. driver.findElement(By.id("company_id")).sendKeys(Environment.SYSTEM_ID);
  47. driver.findElement(By.xpath("//input[@name='username']")).clear();
  48. driver.findElement(By.xpath("//input[@name='username']")).sendKeys(Environment.USERNAME);
  49. driver.findElement(By.id("c_password")).clear();
  50. driver.findElement(By.id("c_password")).sendKeys(Environment.PASSWORD);
  51. System.out.println(" 2 - Info filled");
  52.  
  53. //I removed the rest of the code that finishes the login because
  54. //I don't think it is needed for demonstration
  55.  
  56. }
  57.  
  58. @Test
  59. public void main() {
  60. doLogin(driver, baseURL);
  61. }
  62.  
  63. @After
  64. public void tearDown() throws Exception {
  65. driver.quit();
  66. String verificationErrorString = verificationErrors.toString();
  67. if (!"".equals(verificationErrorString)) {
  68. fail(verificationErrorString);
  69. }
  70. }
  71. }
  72.  
  73. import static org.junit.Assert.fail;
  74.  
  75. import java.util.concurrent.TimeUnit;
  76.  
  77. import org.junit.After;
  78. import org.junit.Before;
  79. import org.junit.Test;
  80. import org.openqa.selenium.By;
  81. import org.openqa.selenium.WebDriver;
  82. import org.openqa.selenium.firefox.FirefoxDriver;
  83. import org.openqa.selenium.ie.InternetExplorerDriver;
  84. import org.openqa.selenium.support.ui.ExpectedConditions;
  85. import org.openqa.selenium.support.ui.Select;
  86. import org.openqa.selenium.support.ui.WebDriverWait;
  87.  
  88. public class Transaction {
  89. private WebDriver driver;
  90. private String baseURL;
  91. private StringBuffer verificationErrors = new StringBuffer();
  92.  
  93. public static String country = "22";
  94. public static String ammount = "500";
  95.  
  96. @Before
  97. public void setUp() throws Exception {
  98. // System.setProperty("webdriver.ie.driver", "../IEDriverServer.exe");
  99. // driver = new InternetExplorerDriver();
  100. driver = new FirefoxDriver();
  101. baseURL = Environment.URL;
  102.  
  103. driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
  104. }
  105.  
  106. public static void doTransaction(WebDriver driver) {
  107. WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
  108. System.out.println("Performing transaction...");
  109.  
  110.  
  111. // This is where the transaction code is ......
  112.  
  113. }
  114.  
  115. @Test
  116. public void transaction() {
  117. //Login first
  118. Login.doLogin(driver, baseURL);
  119.  
  120. // Transaction
  121. doTransaction(driver);
  122. }
  123.  
  124. @After
  125. public void tearDown() throws Exception {
  126. driver.quit();
  127. String verificationErrorString = verificationErrors.toString();
  128. if (!"".equals(verificationErrorString)) {
  129. fail(verificationErrorString);
  130. }
  131. }
  132. }
  133.  
  134. <suite>
  135. <test name="firstTest">
  136. <parameter name="username" value="username1"/>
  137. <parameter name="password" value="password1"/>
  138. ....
  139. </test>
  140. <test name="secondTest">
  141. <parameter name="username" value="username2"/>
  142. <parameter name="password" value="password2"/>
  143. ....
  144. </test>
  145. </suite>
  146.  
  147. @Parameters({"username", "password"})
  148. @BeforeTest
  149. public void beforeTest(String username, String password) {
  150. this.getDriver().get("http://mypage.com");
  151. this.getDriver().findElement(By.id("usernameInputField")).sendKeys(username);
  152. this.getDriver().findElement(By.id("passwordInputField")).sendKeys(password);
  153. ...
  154. }
Add Comment
Please, Sign In to add comment