Skip to content

Commit

Permalink
Bài 7 WebDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
anhtester committed Jul 19, 2023
1 parent 760b222 commit a0e459a
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 28 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<version>4.10.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-http-jdk-client -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-http-jdk-client</artifactId>
<version>4.10.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/anhtester/Bai7_WebDriver/DemoCookies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.anhtester.Bai7_WebDriver;

import com.anhtester.Bai5_Locators.BT_LocatorsCRM;
import com.anhtester.common.BaseTest;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;

import java.util.Set;

public class DemoCookies extends BaseTest {

public static void main(String[] args) {

createBrowser();

driver.get("https://crm.anhtester.com/admin/authentication");
driver.findElement(By.xpath(BT_LocatorsCRM.inputEmail)).sendKeys("admin@example.com");
sleep(1);
driver.findElement(By.xpath(BT_LocatorsCRM.inputPassword)).sendKeys("123456");
sleep(1);
//driver.findElement(By.xpath(BT_LocatorsCRM.buttonLogin)).click();
driver.findElement(By.xpath(BT_LocatorsCRM.inputPassword)).submit();

// Get all cookies
Set<Cookie> cookies = driver.manage().getCookies();

System.out.println(cookies.iterator().next().toString());

//Get cookies current by name
String cookiesCurrent = driver.manage().getCookieNamed("sp_session").getValue();
System.out.println("Current Cookies: " + cookiesCurrent);

closeBrowser();
}

}
39 changes: 39 additions & 0 deletions src/test/java/com/anhtester/Bai7_WebDriver/DemoListWebElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.anhtester.Bai7_WebDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;
import java.util.List;

public class DemoListWebElement {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));

// Navigate to a page
driver.get("https://www.google.com/");
Thread.sleep(1000);
driver.findElement(By.xpath("//textarea[@name='q']")).click();
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys("Selenium Java");
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys(Keys.ENTER);
Thread.sleep(1000);

List<WebElement> listTitle = driver.findElements(By.xpath("//h3"));

for (int i = 0; i < listTitle.size(); i++) {
System.out.println(listTitle.get(i).getText());
//Compare

}

Thread.sleep(1000);
driver.quit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.anhtester.Bai7_WebDriver;

import com.anhtester.common.BaseTest;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.time.Duration;
import java.util.Set;

public class DemoSwitchToNewWindow extends BaseTest {

public static void main(String[] args) {

createBrowser();

driver.get("https://github.com/anhtester/SeleniumMaven042023");
String mainWindow = driver.getWindowHandle();
System.out.println(mainWindow);
sleep(1);
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://google.com");
sleep(1);
driver.switchTo().window(mainWindow); //Chuyển về của sổ mặc định ban đầu

closeBrowser();
}

}
50 changes: 50 additions & 0 deletions src/test/java/com/anhtester/Bai7_WebDriver/DemoWebDriverBasic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.anhtester.Bai7_WebDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.time.Duration;

public class DemoWebDriverBasic {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new EdgeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));

// Navigate to a page
driver.get("https://www.google.com/");
Thread.sleep(1000);
driver.findElement(By.xpath("//textarea[@name='q']")).click();
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys("Selenium Java");
driver.findElement(By.xpath("//textarea[@name='q']")).sendKeys(Keys.ENTER);
Thread.sleep(1000);
driver.findElement(By.xpath("//h3[contains(text(),'[Selenium Java] Bài 4: Cài đặt môi trường Selenium')]")).click();
Thread.sleep(2000);
// Điều hướng về lịch sử trang trước đó
driver.navigate().back();
Thread.sleep(1000);
// Điều hướng đến trang tiếp sau
driver.navigate().forward();
Thread.sleep(1000);
// Làm mới trang hiện tại
driver.navigate().refresh();

// Get the title of the page
String title = driver.getTitle();
// Get the current URL
String url = driver.getCurrentUrl();
// Get the current page HTML source
String html = driver.getPageSource();

System.out.println("Title: " + title);
System.out.println("Current URL: " + url);
System.out.println("Page Sources: " + html);

Thread.sleep(1000);
driver.quit();
}
}
28 changes: 0 additions & 28 deletions src/test/java/com/anhtester/TestSelenium.java

This file was deleted.

57 changes: 57 additions & 0 deletions src/test/java/com/anhtester/common/BaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.anhtester.common;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.time.Duration;

public class BaseTest {

public static WebDriver driver;

public static void createBrowser(){
System.setProperty("webdriver.http.factory", "jdk-http-client");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
}

public static void createBrowser(String browserName){
System.setProperty("webdriver.http.factory", "jdk-http-client");

if(browserName.equals("chrome")){
driver = new ChromeDriver();
}
if(browserName.equals("edge")){
driver = new EdgeDriver();
}
if(browserName.equals("firefox")){
driver = new FirefoxDriver();
}

driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
}

public static void closeBrowser(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
driver.quit();
}

public static void sleep(double second){
try {
Thread.sleep((long) (1000 * second));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit a0e459a

Please sign in to comment.