Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package seletest;
  7.  
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.chrome.ChromeDriver;
  12.  
  13. /**
  14.  *
  15.  * @author Mishuhashi
  16.  */
  17. public class SeleTest extends Thread {
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      * @throws java.lang.InterruptedException
  22.      */
  23.     public static void main(String[] args) throws InterruptedException {
  24.        
  25.         // Set đường dẫn tới của ChromeDriver
  26.         System.setProperty("webdriver.chrome.driver", "webdriver/chromedriver.exe");
  27.  
  28.         WebDriver driver = new ChromeDriver();
  29.  
  30.         // Truy cập trandatdt.dev
  31.         driver.get("https://trandatdt.dev");
  32.  
  33.         // Tìm các trường name/email/message dựa vào id
  34.         WebElement name = driver.findElement(By.id("name"));
  35.         WebElement email = driver.findElement(By.id("email"));
  36.         WebElement message = driver.findElement(By.id("message"));
  37.        
  38.         String a = "1";
  39.  
  40.         // Nhập nội dung
  41.         name.sendKeys(a);
  42.         email.sendKeys("trandatdtxPNT");
  43.         message.sendKeys("Test selenium nek!!!");
  44.  
  45.         // Submit
  46.         // Có 2 cách submit
  47.         // 1 là dùng submit()
  48.         // 2 là select cái button rồi dùng click()
  49.         // => 1 tiện hơn
  50.         message.submit();
  51.        
  52.         // Đợi 5 giây để load (dùng với Ajax).  Mục đích để dùng if/else
  53.         // Ngoài ra tham khảo:
  54.         // https://vananhtooo.wordpress.com/2017/11/13/implicit-wait-explicit-wait-va-fluent-wait-trong-selenium-webdriver/
  55.         Thread.sleep(5000);
  56.        
  57.         // Lấy text trong <div id="msgSubmit">text</div>
  58.         String result = driver.findElement(By.id("msgSubmit")).getText();
  59.        
  60.         if (result.equals("Message Submitted!")) {
  61.             System.out.println("Do other stuff if success");
  62.         }
  63.         else {
  64.             System.out.println("Do other stuff if failed");
  65.         }
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement