Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. import io.github.bonigarcia.wdm.WebDriverManager;
  2. import org.openqa.selenium.*;
  3. import org.openqa.selenium.chrome.ChromeDriver;
  4. import org.openqa.selenium.support.ui.ExpectedCondition;
  5. import org.openqa.selenium.support.ui.ExpectedConditions;
  6. import org.openqa.selenium.support.ui.WebDriverWait;
  7. import org.testng.Assert;
  8. import org.testng.annotations.AfterTest;
  9. import org.testng.annotations.BeforeTest;
  10. import org.testng.annotations.Test;
  11. import io.github.bonigarcia.wdm.WebDriverManager;
  12. import org.openqa.selenium.*;
  13. import org.openqa.selenium.chrome.ChromeDriver;
  14. import org.testng.annotations.AfterTest;
  15. import org.testng.annotations.BeforeTest;
  16. import org.testng.annotations.Test;
  17.  
  18. import java.util.concurrent.TimeUnit;
  19.  
  20. import java.util.concurrent.TimeUnit;
  21.  
  22.  
  23. public class Explicit {
  24. WebDriver driver;
  25.  
  26. @BeforeTest
  27. public void setUp() {
  28. WebDriverManager.chromedriver().setup();
  29. this.driver = new ChromeDriver();
  30. }
  31.  
  32.  
  33. @Test
  34. public void firstTestLoginToJira() throws InterruptedException {
  35. // Fix for - The path to the driver executable must be set by the webdriver.chrome.driver system property
  36. // System.setProperty("webdriver.chrome.driver", "D:\\QA_Auto\\12345\\chromedriver.exe");
  37. // Create a new instance of the Firefox driver
  38. // WebDriver driver = new ChromeDriver();
  39.  
  40. driver.get("https://jira.hillel.it/login.jsp");
  41. driver.findElement(By.xpath("//input[@name='os_username']")).sendKeys("webinar5");
  42. driver.findElement(By.xpath("//input[@name='os_password']")).sendKeys("webinar5");
  43. driver.findElement(By.xpath("//input[@name='login']")).click();
  44. Assert.assertEquals(driver.getCurrentUrl(), "https://jira.hillel.it/secure/Dashboard.jspa");
  45.  
  46. //Thread.sleep(50000);
  47. WebDriverWait wait = new WebDriverWait(driver, 20);//Заменила Thread.sleep() на explicit wait
  48. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@id='create_link']")));
  49.  
  50. driver.findElement(By.xpath("//a[@id='create_link']")).click();
  51.  
  52. //Thread.sleep(30000);
  53. //WebElement element1 =
  54. WebDriverWait wait1 = new WebDriverWait(driver, 20);//Заменила Thread.sleep() на explicit wait
  55. wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='project-field']")));
  56.  
  57. //driver.findElement(By.xpath("//input[@id='project-field']"));
  58. //driver.findElement(By.xpath("//input[@id='project-field']")).clear();//QAAUTO-8(QAAUT-8) выбираем
  59. //driver.findElement(By.xpath("//input[@id='project-field']")).sendKeys("QAAUTO-8 (QAAUT8)");
  60. //driver.findElement(By.xpath("//div[@class='qf-container']")).click();
  61.  
  62. //заменяем предыдуший код на enterText
  63. enterText(By.xpath("//input[@id='project-field']"), "Test", 3, 10);
  64. enterText(By.xpath("//input[@id='issuetype-field']"), "QAAUTO-8", 3, 10);
  65.  
  66. //driver.findElement(By.xpath("//input[@id='issuetype-field']"));
  67. //driver.findElement(By.xpath("//input[@id='issuetype-field']")).clear();
  68. //driver.findElement(By.xpath("//input[@id='issuetype-field']")).sendKeys("Task");
  69. //driver.findElement(By.xpath("//div[@class='qf-container']")).click();
  70.  
  71. //driver.findElement(By.xpath("//input[@id='summary']")).sendKeys("THIS IS TEST FOR QA AUTOMATION");
  72. //driver.findElement(By.xpath("//textarea[@id='description']")).sendKeys("THIS IS TEST FOR QA AUTOMATION in Jira");
  73. enterText(By.xpath("//input[@id='summary']"), "This is an automatic test.", 3, 10);
  74. enterText(By.xpath("//textarea[@id='description']"), "This is an automatic test.", 3, 10);
  75.  
  76. //Thread.sleep(30000);
  77.  
  78. WebDriverWait wait2 = new WebDriverWait(driver, 20);//Заменила Thread.sleep() на explicit wait
  79. wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='create-issue-submit']")));
  80.  
  81. driver.findElement(By.xpath("//input[@id='create-issue-submit']")).click();
  82. //Thread.sleep(2000);
  83. //assertTrue(true,"has been successfully created");
  84. //assertTrue(driver.findElement( By.xpath("//div[@class='aui-message closeable aui-message-success aui-will-close']")).isDisplayed());
  85.  
  86. driver.quit();
  87.  
  88. }
  89.  
  90.  
  91. private WebElement sendKeys(By element, int retry, int timeoutSeconds) {
  92. for (int i = retry; i > 0; i--) {
  93. try {
  94. driver.findElement(element);
  95. } catch (Exception ex) {
  96. try {
  97. Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutSeconds));
  98. driver.findElement(element);
  99. } catch (Exception ex2) {
  100. System.out.println("Searching element" + element.toString() + ". Retry - " + (retry - i));
  101. continue;
  102. }
  103. }
  104. return driver.findElement(element);
  105. }
  106. return driver.findElement(element);
  107. }
  108.  
  109. private void click(By element, int retry, int timeoutSeconds) {
  110. for (int i = retry; i > 0; i--) {
  111. try {
  112. System.out.println("Searching element" + element.toString() + ". Retry - " + (retry - i));
  113. driver.findElement(element).click();
  114. break;
  115. } catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ex) {
  116. try {
  117. System.out.println("Searching element" + element.toString() + ". Retry - " + (retry - i));
  118. Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutSeconds));
  119. driver.findElement(element).click();
  120. break;
  121. } catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException | InterruptedException ex2) {
  122. continue;
  123. }
  124. }
  125. }
  126. }
  127.  
  128. private void enterText(By element, String text, int retry, int timeoutSeconds) {
  129. for (int i = retry; i > 0; i--) {
  130. try {
  131. System.out.println("Searching element" + element.toString() + ". Retry - " + (retry - i));
  132. driver.findElement(element).sendKeys(text);
  133. break;
  134. } catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ex) {
  135. try {
  136. System.out.println("Searching element" + element.toString() + ". Retry - " + (retry - i));
  137. Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutSeconds));
  138. driver.findElement(element).sendKeys(text);
  139. break;
  140. } catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException | InterruptedException ex2) {
  141. continue;
  142. }
  143. }
  144. }
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement