-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/com/anhtester/Bai7_WebDriver/DemoCookies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/test/java/com/anhtester/Bai7_WebDriver/DemoListWebElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/anhtester/Bai7_WebDriver/DemoSwitchToNewWindow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
src/test/java/com/anhtester/Bai7_WebDriver/DemoWebDriverBasic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |