Pages - Menu

2019年10月31日 星期四

[Katalon Studio]判斷動態元素狀態

前言

這部分是我最混亂的,判斷element 顯示 or 不顯示 or 按下 …等
寫了好幾種判斷方式,但最後有效的反而是第四種   driver.findElements



正文

1.使用WebUI判斷元素狀態(只適合靜態元素)
這部分需要Object Repository配合(Fig.1)

(Fig.1)
先將xpath檢查的元素寫進去Test Object裡面
再來就是判斷元素的狀態了

     if (WebUI.verifyElementClickable(findTestObject('Page_Windchill/searchElement'), FailureHandling.CONTINUE_ON_FAILURE)==true) {
         //System.out.println("searchElement is True.")
         selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')
     } else {
         //System.out.println("searchElement is False.")
         WebUI.executeJavaScript(jsCode,
             null)
        selenium.type('xpath=.//*[contains(@ng-repeat,"item in group.content")][1]//td[4]//input',item_no)
         selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')
     }

除了verifyElementClickable以外還有許多的函數,
完整文章請參考  Katalon Studio 之 Test Case,使用介紹
下面摘錄部分內容

// 等待,直到元素具有特定名稱和值的屬性。
WebUI.waitForElementAttributeValue(TestObject to, String attributeName, String attributeValue, int timeOut)
//等待元素在特定的時間內單擊(以秒為單位)。
WebUI.waitForElementClickable(TestObject to, int timeOut)
//等待,直到元素具有特定名稱的屬性。
WebUI.waitForElementHasAttribute(TestObject to, String attributeName, int timeOut)
//等待元素在時間內不可點擊(以秒為單位)。
WebUI.waitForElementNotClickable(TestObject to, int timeOut)
// 等待元素沒有特定名稱的屬性
WebUI.waitForElementNotHasAttribute(TestObject to, String attributeName, int timeOut)
// 等待元素在時間內不出現。
WebUI.waitForElementNotPresent(TestObject to, int timeOut)
//等待,直到元素在超時時間內不顯示。
WebUI.waitForElementNotVisible(TestObject to, int timeOut)
//等待元素在時間內出現。
WebUI.waitForElementPresent(TestObject to, int timeOut)
//等待元素在時間內可見。
WebUI.waitForElementVisible(TestObject to, int timeOut)
//等待圖像出現在頁面上
WebUI.waitForImagePresent(TestObject to, int timeOutInSeconds)


2.使用WebUI判斷元素狀態,跟上面類似,適合拿來做動態元素判斷。
需先新增Test Object ,這邊新增一個取名為 clickItem。

    TestObject clickItem = TestObject("clickItem").addProperty('xpath', ConditionType.EQUALS, 'xpath=/a[contains(text(),"' + item_no + '")]')
if(WebUI.verifyElementPresent(clickItem,5) == false ){
    selenium.type('id=keywordkeywordField_SearchTextBox', item_no)
    selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')
}

3.使用WebUI判斷元素狀態,跟上面不太一樣,這個是完全寫code適合拿來做動態元素判斷,
不需要再到Object 裡面新增Test Object,直接靠程式新增。但是,實務上在檢查的時候,一直跑不順。

TestObject to = new TestObject("linkItem")
to.addProperty("xpath",ConditionType.EQUALS,xpathLink) 


if(!WebUI.verifyElementPresent(to,10)){
     System.out.println(  item_no + " Not Found")
     delayTime(10000)
     WebUI.executeJavaScript(jsCode,
         null)
    selenium.type('id=keywordkeywordField_SearchTextBox', item_no)
    selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')           
    
     if(!WebUI.verifyElementClickable(to,FailureHandling.OPTIONAL)){
         System.out.println(  item_no + " Not Found again")
         WebUI.refresh()
         delayTime(5000)
         WebUI.executeJavaScript(jsCode,
             null)
        selenium.type('id=keywordkeywordField_SearchTextBox', item_no)
        selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')
     }
}

4.使用driver.findElements 來找元素是否存在,可用來檢查動態元素

import org.openqa.selenium.By
string xpathLink = String.format("//table[contains(@class,'x-grid3-row-table')]”)
if (driver.findElements(By.xpath(xpathLink)).size()!=0){
         C_print("Find it.")       
     }else{
         selenium.click('xpath=//button[contains(@class,\' x-btn-text\') and contains(text(),\'搜尋\')]')
     }

也可使用
By.className 、By.cssSelector、By.Id、By.linkText、By.name、By.tagName
 
 

ref.
How to handle WebUI.click() and WebUI.setText() built in keywords using “XPATH”
Web Driver Find Element By
WebDriver: check if an element exists? [duplicate]
Handling static and dynamic Test Objects
[WebUI] Verify Element Present
How to find elements by class using katalon studio?
if element not found skip to next iteration else continue statement
Failure Handling
20+個Selenium自動化測試常用指令範例 (Java)

沒有留言:

張貼留言