Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package seletest;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.chrome.ChromeDriver;
- /**
- *
- * @author Mishuhashi
- */
- public class SeleTest extends Thread {
- /**
- * @param args the command line arguments
- * @throws java.lang.InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // Set đường dẫn tới của ChromeDriver
- System.setProperty("webdriver.chrome.driver", "webdriver/chromedriver.exe");
- WebDriver driver = new ChromeDriver();
- // Truy cập trandatdt.dev
- driver.get("https://trandatdt.dev");
- // Tìm các trường name/email/message dựa vào id
- WebElement name = driver.findElement(By.id("name"));
- WebElement email = driver.findElement(By.id("email"));
- WebElement message = driver.findElement(By.id("message"));
- String a = "1";
- // Nhập nội dung
- name.sendKeys(a);
- email.sendKeys("trandatdtxPNT");
- message.sendKeys("Test selenium nek!!!");
- // Submit
- // Có 2 cách submit
- // 1 là dùng submit()
- // 2 là select cái button rồi dùng click()
- // => 1 tiện hơn
- message.submit();
- // Đợi 5 giây để load (dùng với Ajax). Mục đích để dùng if/else
- // Ngoài ra tham khảo:
- // https://vananhtooo.wordpress.com/2017/11/13/implicit-wait-explicit-wait-va-fluent-wait-trong-selenium-webdriver/
- Thread.sleep(5000);
- // Lấy text trong <div id="msgSubmit">text</div>
- String result = driver.findElement(By.id("msgSubmit")).getText();
- if (result.equals("Message Submitted!")) {
- System.out.println("Do other stuff if success");
- }
- else {
- System.out.println("Do other stuff if failed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement