Authencation Alert

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.4 kB
17
Indexable
package webdriver;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Topic_12_Handle_Alert {
	WebDriver driver;
	String projectPath = System.getProperty("user.dir");
	String osName = System.getProperty("osName");
	Alert alert ;
	String firefoxAuthenAutoIT = projectPath  + "\\autoITScripts\\" + "authen_firefox.exe";
	String chormeAuthenAutoIT = projectPath  + "\\autoITScripts\\" + "authen_chrome.exe";


	@BeforeClass
	public void beforeClass() {
		System.setProperty("webdriver.gecko.driver", projectPath + "\\browserDrivers\\geckodriver.exe");
		driver = new FirefoxDriver();
		
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		
	}


	public void TC_01_Acept_Alert() {
		driver.get("https://automationfc.github.io/basic-form/index.html");
		sleepInsecond(1);
		driver.findElement(By.xpath("//button[text()='Click for JS Alert']")).click();
		
		//Switch to Alert (Khi alert đang xuất hiện
		alert= driver.switchTo().alert();
		sleepInsecond(1);
		
		//Verify Alert title trước khi accept
		Assert.assertEquals(alert.getText(),"I am a JS Alert");
		
		//Accept 1 alert
		alert.accept();
		sleepInsecond(1);
		
		//Verify Accept alert thành công
		Assert.assertEquals(driver.findElement(By.cssSelector("p#result")).getText(), "You clicked an alert successfully");
	}
	

	
	public void TC_02_Confirm_Alert(){
		driver.get("https://automationfc.github.io/basic-form/index.html");
		sleepInsecond(1);
		driver.findElement(By.xpath("//button[text()='Click for JS Confirm']")).click();
		alert = driver.switchTo().alert();
		sleepInsecond(1);
		
		Assert.assertEquals(alert.getText(), "I am a JS Confirm");
		
		//Cancle 1 alert
		alert.dismiss();
		sleepInsecond(1);
		
		Assert.assertEquals(driver.findElement(By.cssSelector("p#result")).getText(), "You clicked: Cancel");
		
		
	}
		
	
	public void TC_03_Prompt_Alert(){
		driver.get("https://automationfc.github.io/basic-form/index.html");
		sleepInsecond(1);
		String keyword = "Tran Thuong"; 
		driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click();
		alert = driver.switchTo().alert();
		
		Assert.assertEquals(alert.getText(),"I am a JS prompt");
		
		alert.sendKeys(keyword);
		alert.accept();
		
		sleepInsecond(1);
		
		Assert.assertEquals(driver.findElement(By.cssSelector("p#result")).getText(), "You entered: "+ keyword);
	}
		
	
	
	public void TC_04_Accept_Alert_Login_Guru99(){
		driver.get("https://demo.guru99.com/V1/index.php");
		sleepInsecond(1);
		driver.findElement(By.xpath("//input[@type='submit']")).click();
		sleepInsecond(1);
		alert = driver.switchTo().alert();
		Assert.assertEquals(alert.getText(), "User is not valid");
		
		alert.accept();
		sleepInsecond(3);
		driver.getCurrentUrl();
		Assert.assertEquals(driver.getCurrentUrl(), "https://demo.guru99.com/V1/index.php");
		
	}
	
	
	public void TC_05_Authencation_Alert_Tructiep(){
		//Paste hẳn Username/ password vào URL trước khi nó ra
		//URL: http://the-internet.herokuapp.com/basic_auth
		//Username/password: admin/admin
		//http://admin/admin@the-internet.herokuapp.com/basic_auth
		driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");
		sleepInsecond(1);
		Assert.assertTrue(driver.findElement(By.cssSelector("div.example>p")).getText().contains("Congratulations! You must have the proper credentials."));
	}
	
	
	//Trường hợp này dùng khi mình đang ở site A rồi click vào button để chuyển sang trang B
	//ở trag B Bắt mình nhập username và pass
	//--> Cách xử lý: tại màn hình A lấy URL của button--> Nhập User/Pass vào sau đó với get ra để vào màn hình B
	public void TC_06_Authencation_Alert_getlink() {
		driver.get("http://the-internet.herokuapp.com/");
		sleepInsecond(1);	
		String basicAuthenURL = driver.findElement(By.xpath("//a[text()='Basic Auth']")).getAttribute("href");
		System.out.println(basicAuthenURL);
		
		driver.get(getAuthenticationURL(basicAuthenURL, "admin", "admin"));
		sleepInsecond(5);	
		Assert.assertTrue(driver.findElement(By.cssSelector("div.example>p")).getText().contains("Congratulations! You must have the proper credentials."));
	}
	
	//Case ngoại lệ: Có 1 số trag đặc biệt k work với cách truyền Usernam/pass
	//Bắt buộc nhập 
	//--> Cách xử lý: Dùng AutoIT (CHỉ support cho win, k hỗ trợ MAc và Linux)
	// Nhập trực tiếp User/pass: Nhập user xong tab để chuyển qua pass
	@Test
	public void TC_07_Authencation_Alert_AutoIT() throws IOException {
		//Bật scrips của AutoIT trước khi mở site chứa Authencaution Alert
		//Câu lệnh thực thi 1 file exe trong code java dùng hàm Runtime
		//Hàm Runtime này trong trường hợp k tì thấy file thì sẽ thrown ra exception nó sẽ chờ
		if (driver.toString().contains("firefox")) {
			Runtime.getRuntime().exec(new String[]{  firefoxAuthenAutoIT , "admin" , "admin" });
		} else {
			Runtime.getRuntime().exec(new String[]{  chormeAuthenAutoIT , "admin" , "admin" });

	
	
		
		driver.get("http://the-internet.herokuapp.com/basic_auth");
		sleepInsecond(5);	
		Assert.assertTrue(driver.findElement(By.cssSelector("div.example>p")).getText().contains("Congratulations! You must have the proper credentials."));
		}
		}
	public String getAuthenticationURL(String basicAuthenURL, String username, String password ) {
		String[] authenURLArray = basicAuthenURL.split("//");
		basicAuthenURL= authenURLArray[0] + "//" + username+ ":" + password + "@" + authenURLArray[1];
		System.out.println(basicAuthenURL);
		return basicAuthenURL;
		
		
		
		
	}
	@AfterClass
	public void afterClass() {
		driver.quit();
	}
	
	// Sleep cứng (Static wait}admin	admin
	
	public void sleepInsecond(long timeInsecond) {
		try {
				Thread.sleep(timeInsecond *1000);
		} catch (InterruptedException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
	
		}
		
	}