
2.1 Xpath tốt đối(Absolute XPath)
Đây là biện pháp thẳng nhằm tra cứu phần tử, tuy nhiên nhược điểm của XPath hoàn hảo là nếu bao gồm bất kỳ đổi khác nào được thực hiện trong đường dẫn của bộ phận thì XPath sẽ ảnh hưởng lỗi.Bạn đang xem: Xpath là gì
đặc điểm chính của XPath là nó bước đầu bằng vệt gạch chéo cánh đơn (/), có nghĩa là chúng ta cũng có thể lựa chọn phần tử trường đoản cú nút cội.
VD: /html/body/div/div/header/div/div<4>/form/div<1>/input

2.2 Xpath tương đối(Relative sầu Xpath)
Đối với Xpath tương đối, băng thông bước đầu từ nửa kết cấu DOM HTML. Nó ban đầu bằng dấu gạch chéo cánh kxay (//), tức là nó hoàn toàn có thể search kiếm phần tử ở ngẫu nhiên đâu bên trên trang web.
VD://input<
id='search'>

3.1 Xpath cơ bản(Tagname & Attribute và Value -> Kiểm tra attribute là duy nhất)
//tagname<attribute=‘value’>VD:1. //input<
id='email'>2. //input<
title='E-Mail Address'>3. //input<
name='login

Priority
Unique + MeaningAttribute: Id/Class/NameAttribute value meaning- relate to lớn field nameNOT(Id/Class/Name)=> Any attribute + Unique + Related lớn field nameLink - Không áp dụng với attribute = href(server)3.2 Parent node(Lấy trường đoản cú đời phụ thân trở xuống)
lúc cần thiết định danh được element bởi những thẻ với attribute của nó:
//parent-tagname<
attribute=‘value’>//child-tagname<
attribute=‘value’>
VD:1. //div<
class='footer'>//a<
title='Site Map'>2. //div<
class='footer-container'>//a<
title='Site Map'>
3.3 Lấy tốt đối(=)
text()=: chứa quý giá tuyệt vời và hoàn hảo nhất trong chuỗiVD: //div<
class'footer'>//a
attribute()=: đựng quý giá tuyệt đối vào attribute
VD: //div<
class='footer-container'>//a<
title='Site Map'>
3.4 Lấy tương đối(,)
contains(): text() || attribute(value) => Chạy chậm trễ hơn do nó quét phạm vi rộng lớn hơnVD: //div<
class='footer-container'>//a

3.5 Starts-with: text() ||
attribute
VD:1. //div<
class='footer-container'>//a
class='footer-container'>//a<
starts-with(
title,'Site Map')>
3.6 Hàm text()
Lúc automation test ở level là Function UI (giả lập hành vi của End User) thì thao tác cùng với text rất nhiều.Lúc làm việc cùng với text bao gồm 5 loại:1. text()=’…’
text nằm ở chính node(tagname) kia - không tồn tại phía bên trong child node hoặc dạng nested text(text lồng nhau các bên trong một thẻ).Lấy text tuyệt đối: không tồn tại khoảng chừng trắng/xuống dòng/tab sống đầu hoặc cuối chuỗi.Xem thêm: Shui Là Gì - Các Lời Khuyên Về Phong Thủy

2. contains(text(),’…‘)
text nằm trong bao gồm node(tagname) kia hoặc bên dưới dạng nested text mà lại text bắt buộc nằm ở vị trí trước tiên.text này còn có khoảng trắng/xuống dòng/tab ở đầu cái hoặc cuối text vẫn work được.Nếu text ở thẳng trong child node thì không lấy được.Không dùng contains(text()=”) cơ mà dùng contains(text(),”).
3. contains(.,’…‘)
text nằm trong chính node (tagname) kia, bên trong child node sống bất cứ vị trí như thế nào, hoặc nested text ngơi nghỉ bất kỳ địa chỉ làm sao.text này còn có khoảng trắng/xuống dòng/tab ngơi nghỉ đầu hoặc cuối chuỗi vẫn work được.sử dụng contains(.,”) ko sử dụng contains(.=”).
4. contains(string(),’…‘)
text vị trí bao gồm node (tagname) kia, nằm trong child node làm việc bất kỳ vị trí làm sao, hoặc nested text cũng worktext này có khoảng chừng trắng/xuống dòng/tab nghỉ ngơi đầu hoặc cuối chuỗi vẫn work được.Dùng contains(string(),”) ko sử dụng contains(string()=”).
5. concat()
text nằm tại chủ yếu node (tagname) kia, phía bên trong child node ngơi nghỉ ngẫu nhiên địa chỉ như thế nào, hoặc nested text cũng worktext này có khoảng trắng/xuống dòng/tab ngơi nghỉ đầu hoặc cuối chuỗi vẫn work được.Nên sử dụng cùng với text có khi tất cả chứa cam kết từ ”.
package webdriver;import java.util.Random;import java.util.concurrent.TimeUnit;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_02_Xpath_Css_PartII { WebDriver driver; private int randomInt;
BeforeClass public void beforeClass() driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().window().maximize();
Test public void Login_01_Empty_Email_And_Password() driver.get("http://live.demoguru99.com/"); driver.findElement(By.xpath("https://div<
class='footer'>//a<
title='My Account'>")).click(); driver.findElement(By.xpath("https://*<
id='email'>")).sendKeys(""); driver.findElement(By.xpath("https://*<
id='pass'>")).sendKeys(""); driver.findElement(By.xpath("https://*<
id='send2'>")).click(); Assert.assertEquals(driver.findElement(By.xpath("https://*<
id='advice-required-entry-email'>")).getText(), "This is a required field."); Assert.assertEquals(driver.findElement(By.xpath("https://*<
id='advice-required-entry-pass'>")).getText(), "This is a required field.");
Test public void Login_02_Invalid_Email() driver.get("http://live sầu.demoguru99.com/"); driver.findElement(By.xpath("https://div<
class='footer'>//a<
title='My Account'>")).click(); driver.findElement(By.xpath("https://*<
id='email'>")).sendKeys("123
2333"); driver.findElement(By.xpath("https://*<
id='pass'>")).sendKeys("123456"); driver.findElement(By.xpath("https://*<
id='send2'>")).click(); Assert.assertEquals(driver.findElement(By.xpath("https://*<
id='advice-validate-email-email'>")).getText(), "Please enter a valid gmail address. For example johndoe
domain.com.");
Test public void Login_03_Invalid_Password_less6character() driver.get("http://live sầu.demoguru99.com/"); driver.findElement(By.xpath("https://div<
class='footer'>//a<
title='My Account'>")).click(); driver.findElement(By.xpath("https://*<
id='email'>")).sendKeys("automation
gmail.com"); driver.findElement(By.xpath("https://*<
id='pass'>")).sendKeys("123"); driver.findElement(By.xpath("https://*<
id='send2'>")).click(); Assert.assertEquals(driver.findElement(By.xpath("https://div<
id='advice-validate-password-pass'>")).getText(), "Please enter 6 or more characters without leading or trailing spaces.");
Test public void Login_04_Correct_Email_Incorrect_Password() driver.get("http://live sầu.demoguru99.com/"); driver.findElement(By.xpath("https://div<
class='footer'>//a<
title='My Account'>")).click(); driver.findElement(By.xpath("https://*<
id='email'>")).sendKeys("automation
gmail.com"); driver.findElement(By.xpath("https://*<
id='pass'>")).sendKeys("123123123"); driver.findElement(By.xpath("https://*<
id='send2'>")).click(); Assert.assertEquals(driver.findElement(By.xpath("https://li<
class='error-msg'>//span")).getText(), "Invalid login or password.");
Test public void Login_05_Create_A_New_Account() throws InterruptedException { driver.get("http://live sầu.demoguru99.com/"); driver.findElement(By.xpath("https://div<
class='footer'>//a<
title='My Account'>")).click(); Thread.sleep(3000); driver.findElement(By.xpath("https://span