- Sử dụng plugins cho SeleniumLibrary
SeleniumLibraryEx.py:
from re import match, search, DOTALL
from typing import List
from robot.api.deco import keyword
from robot.api.logger import warn
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.event_firing_webdriver import (
EventFiringWebElement
)
from SeleniumLibrary import ElementFinder, LibraryComponent
class SeleniumLibraryEx(LibraryComponent):
def __init__(self, ctx, warnings=True):
LibraryComponent.__init__(self, ctx)
self.element_finder = ElementFinder(ctx)
self._warnings = [] if warnings else None
@keyword()
def get_all_texts(self, locator) -> List[str]:
"""Returns a list of texts of WebElements matching the locator
"""
elements = self.find_elements(locator)
return [element.text for element in elements]
Library SeleniumLibrary plugins=${EXECDIR}/Libs/SeleniumLibraryEx.py timeout=30 run_on_failure=SeleniumLibrary.CapturePageScreenshot
2. WebCore
*** Settings ***
Documentation - Chủ yếu được dùng ở tầng UIKeyword, và VerificationKeyword,
... - Có thể được dùng ở tầng HighLevelKeyword nhưng nên hạn chế
Resource WebBaseLibraries.resource
Resource Utils.resource
Resource CommonVariable.resource
*** Variables ***
*** Keywords ***
Scroll Page To Location
[Arguments] ${x_location} ${y_location}
Sleep 2s
SeleniumLibrary.Execute JavaScript window.scrollTo(${x_location},${y_location})
Sleep 2s
Element Text Should End With
[Arguments] ${locatorXpath} ${endsWith}
[Documentation] This is an utility keyword used in in order to verify if the text value of element ends with expected value
...
... | locatorXpath - xpath that localizes a web element
... | endsWith - expected end of localized element
${locatorText}= WEB-Get Text ${locatorXpath}
Should End With ${locatorText} ${endsWith}
Element Text Should Start With
[Arguments] ${locatorXpath} ${startsWith}
[Documentation] This is an utility keyword used in in order to verify if the text value of element starts with expected value
...
... | locatorXpath - xpath that localizes a web element
... | startsWith - expected beginning of localized element
${locatorText}= WEB-Get Text ${locatorXpath}
Should Start With ${locatorText} ${startsWith}
Run keyword with retry
[Arguments] ${keyword} @{args}
[Documentation] This keyword is used to run keyword with retry option (used in case when keyword fails because of loading elements)
Wait Until Keyword Succeeds 20s 1s ${key_word} @{args}
WEB-Input Text
[Arguments] ${locator} ${value}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
SeleniumLibrary.Wait Until Element Is Enabled ${locator} ${timeout}
SeleniumLibrary.Input Text ${locator} ${value} clear=True
WEB-Input Password
[Arguments] ${locator} ${value}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
SeleniumLibrary.Wait Until Element Is Enabled ${locator} ${timeout}
SeleniumLibrary.Input Password ${locator} ${value} clear=True
WEB-Input Text Last Element
[Arguments] ${locator} ${value}
[Documentation] Do not input "xpath=" into ${locator}
${item_index}= WEB-Get Element Count xpath=${locator}
WEB-Input Text xpath=(${locator})[${item_index}] ${value}
WEB-Click Element
[Arguments] ${locator} ${time}=${timeout}
SeleniumLibrary.Wait Until Page Contains Element ${locator} timeout=${time}
Comment wait_and_Highlight_Page_Contains_Element ${locator}
SeleniumLibrary.Click Element ${locator}
WEB-Click Element If Visible
[Arguments] ${locator}
${is_visible} = Run Keyword And Return Status SeleniumLibrary.Wait Until Element Is Visible ${locator}
Run Keyword If ${is_visible} WEB-Click Element ${locator}
RETURN ${is_visible}
WEB-Click Last Element
[Arguments] ${locator}
[Documentation] Do not input "xpath=" into ${locator}
${item_index}= WEB-Get Element Count xpath=${locator}
Run Keyword If '${item_index}' != '0' WEB-Click Element xpath=(${locator})[${item_index}]
Run Keyword If '${item_index}' == '0' WEB-Click Element xpath=(${locator})[1]
WEB-Click Last Element If Visible
[Arguments] ${locator}
[Documentation] Do not input "xpath=" into ${locator}
${item_index}= WEB-Get Element Count xpath=${locator}
Run Keyword If '${item_index}'!='0' WEB-Click Element xpath=(${locator})[${item_index}]
WEB-Double Click Element
[Arguments] ${locator} ${timeWait}=60s
SeleniumLibrary.Wait Until Page Contains Element ${locator}
Double Click Element ${locator}
WEB-Scroll And Click Element
[Arguments] ${locator} ${timeWait}=60s
Wait Until Element Is Visible ${locator}
Wait Until Element Is Enabled ${locator}
Scroll Element Into View ${locator}
Click Element ${locator}
Wait In Specific Time 2s
WEB-Click Link
[Arguments] ${locator}
Wait Until Element Is Visible ${locator}
Click Link ${locator}
WEB-Click Element by Label
[Arguments] ${label}
WEB-Click Last Element //*[text()="${label}"]
WEB-Scroll Element Into View
[Arguments] ${locator}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
${vertical_position} SeleniumLibrary.Get Vertical Position ${locator}
${vertical_position} Evaluate ${vertical_position}-200
Scroll Page To Location 0 ${vertical_position}
WEB-Click Button
[Arguments] ${locator}
SeleniumLibrary.Wait Until Element Is Visible ${locator} 60s
SeleniumLibrary.Wait Until Element Is Enabled ${locator} 60s
SeleniumLibrary.Click Button ${locator}
WEB-Select From List By Value
[Arguments] ${locator} ${index_quantity}
SeleniumLibrary.Wait Until Element Is Visible ${locator}
SeleniumLibrary.Select From List By Value ${locator} ${index_quantity}
WEB-Select Radio Button
[Arguments] ${location} ${value}=${EMPTY}
SeleniumLibrary.Wait Until Element Is Visible name=${location}
SeleniumLibrary.Select Radio Button ${location} ${value} #Select top existed Shipping Address
WEB-Press Enter Key
[Arguments] ${locator}
SeleniumLibrary.Wait Until Element Is Visible ${locator}
SeleniumLibrary.Press Keys ${locator} ENTER
WEB-Clear Field Of Characters
[Arguments] ${locator} ${character_count}
[Documentation] This keyword pushes the delete key (ascii: \8) a specified number of times in a specified field.
FOR ${index} IN RANGE ${character_count}
SeleniumLibrary.Press Key ${locator} \\8
END
WEB-Input Into Text Field
[Arguments] ${locator} ${text}
[Documentation] Keyword is just an input text keyword. That clears the text field dynamically. This function should be used in case input number into text field.
${field_text} SeleniumLibrary.Get Value ${locator}
${field_text_length} Get Length ${field_text}
WEB-Clear Field Of Characters ${locator} ${field_text_length}
SeleniumLibrary.Press Key ${locator} ${text}
WEB-Get Text
[Arguments] ${locator}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
Comment SeleniumLibrary.Scroll Element Into View ${locator}
SeleniumLibrary.Wait Until Element Is Visible ${locator}
${text} SeleniumLibrary.Get Text ${locator}
log ${text} #ghi log text tra ra
RETURN ${text}
WEB-Press Tab Key
[Arguments] ${locator}
[Documentation] Refer ascii at: https://en.wikipedia.org/wiki/ASCII
SeleniumLibrary.Press Key ${locator} \\09 #Tab
Press Enter Key
[Arguments] ${locator}=None
[Documentation] Refer ascii at: https://en.wikipedia.org/wiki/ASCII
SeleniumLibrary.Press Keys ${locator} ENTER #Tab
Kill Chromedriver
Run Keyword If '${RUNNING_ON_JENKIN}'=='N' OperatingSystem.Run taskkill /F /IM chromedriver.exe
Run Keyword If '${RUNNING_ON_JENKIN}'=='N' OperatingSystem.Run taskkill /F /IM geckodriver.exe
WEB-Stop Screenshot On Failure
[Documentation] Disable unneccessary warning message: Capture page screenshot
SeleniumLibrary.Register Keyword To run on failure Nothing
WEB-Start Screenshot On Failure
Register Keyword To run on failure capture page screenshot
WEB-Get Number of Open Tabs
[Documentation] Get number of open tabs
@{windows}= List Windows
${number_of_tabs}= Get Length ${windows}
# ${indexLast}= Evaluate ${numWindows}-1
# Select Window @{windows}[${indexLast}]
RETURN ${number_of_tabs}
Select Tab
[Arguments] ${tab_index}=1
[Documentation] Select Tab by index number
@{windows}= List Windows
${tab_index}= Evaluate ${tab_index}-1
# Should Be True ${tab_index} > 1
Select Window @{windows}[${tab_index}]
Check If Element Xpath Exists
[Arguments] ${locator}
[Documentation] DuongNQ them keyword nay thay cho keyword cu
${is_show} Run Keyword And Return Status SeleniumLibrary.Wait Until Page Contains Element ${locator} 2s
RETURN ${is_show}
WEB-Common-Click First Element
[Arguments] ${locator}
[Documentation] Do not input "xpath=" into ${locator}
WEB-Click Element xpath=(${locator})[1]
WEB-Input Text First Element
[Arguments] ${locator} ${value}
[Documentation] Do not input "xpath=" into ${locator}
WEB-Input Text xpath=(${locator})[1] ${value}
Get DateTime
WEB-Hover On Element
[Arguments] ${locator}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
SeleniumLibrary.Mouse Over ${locator}
WEB-Wait Until Element Is Visible
[Arguments] ${locator} ${time}=${timeout}
${is_visible} SeleniumLibrary.Wait Until Element Is Visible ${locator} timeout=${time}
RETURN ${is_visible}
WEB-Common-Get Element Attribute
[Arguments] ${locator} ${attribute}
[Documentation] Arguments:
...
... [ locator | attribute ]
...
... Returns the value of attribute from the element locator.
...
... See the `Locating elements` section for details about the locator syntax.
WEB-Wait Until Page Contains Element ${locator}
${txtValue} SeleniumLibrary.Get Element Attribute ${locator} ${attribute}
RETURN ${txtValue}
WEB-Get Element Attribute
[Arguments] ${locator} ${attribute}
[Documentation] Get Element Attribute
...
... Source:SeleniumLibrary <test library>
...
... Arguments:
...
... [ locator | attribute ]
...
... Returns the value of attribute from the element locator.
...
... See the `Locating elements` section for details about the locator syntax.
${txt} SeleniumLibrary.Get Element Attribute ${locator} ${attribute}
RETURN ${txt}
WEB-Element Should Be Enabled
[Arguments] ${locator}
SeleniumLibrary.Wait Until Element Is Enabled ${locator}
WEB-Press Keys
[Arguments] ${locator} ${keys}
[Documentation] Arguments:
... [ locator=None | *keys ]
SeleniumLibrary.Press Keys ${locator} ${keys} #Tab
WEB-Wait Until Page Does Not Contain Element
[Arguments] ${locator} ${time}=${timeout}
${is_visible} SeleniumLibrary.Wait Until Page Does Not Contain Element ${locator} timeout=${time}
RETURN ${is_visible}
WEB-Clear Element Text
[Arguments] ${locator}
SeleniumLibrary.Clear Element Text ${locator}
WEB-Get Element Count
[Arguments] ${locator}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
${count} SeleniumLibrary.Get Element Count ${locator}
RETURN ${count}
WEB-Wait Until Page Contains Element
[Arguments] ${locator} ${time}=${timeout}
${is_visible} SeleniumLibrary.Wait Until Page Contains Element ${locator} timeout=${time}
RETURN ${is_visible}
WEB-Get All Texts
[Arguments] ${locator}
SeleniumLibrary.Wait Until Page Contains Element ${locator}
SeleniumLibrary.Scroll Element Into View ${locator}
SeleniumLibrary.Wait Until Element Is Visible ${locator}
${text} get all texts ${locator}
RETURN ${text}
OPEN BROWSER AND DELETE COOKIES
[Arguments] ${url}=${URL} ${testBrowser}=${RUN_BROWSER} ${ALIAS}=a1
log ${TEMPDIR}
SeleniumLibrary.Open Browser ${url} ${testBrowser} alias=${ALIAS} options=add_argument("--start-maximized");add_experimental_option("detach", True) #remote_url=${REMOTE_URL}
OPEN BROWSER AND CONFIG DOWNLOAD FILE
[Arguments] ${url}=${URL}
[Documentation] Mô tả : Mở trình duyệt và download đường dẫn trong biến ${download_directory}, biến ${download_directory} được set test varibale => gọi ra là biết thư mục nằm ở đâu
...
... Chrome only
...
... Author: TuyenLe
...
... Date created: 15:43 26/05/2023
${now} Get Time epoch
${download directory} Join Path ${OUTPUT DIR} downloads_${now}
Set Test Variable ${download directory}
Create Directory ${download directory}
${chrome options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${disabled} Create List Chrome PDF Viewer
Call Method ${chrome_options} add_argument --start-maximized
${prefs} Create Dictionary download.default_directory=${download directory} plugins.plugins_disabled=${disabled} detach=True
Call Method ${chrome options} add_experimental_option prefs ${prefs}
Call Method ${chrome options} add_experimental_option detach ${True}
SeleniumLibrary.Open browser url=${URL} browser=Chrome options=${chrome options}
Open browser reusing existing browser
[Arguments] ${testBrowser}=${RUN_BROWSER}
[Documentation] Vùi lòng cập nhật thư viện bằng lên bên dưới cho lần đầu tiên
... "pip install robotframework-seleniumlibrary==6.1.0"
...
... "pip install selenium==4.13.0"
...
... Chạy cái này trước run lệnh này trong CMD
...
... "C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=6969 --user-data-dir="C:\temp\gdata"
...
... Sửa biến trong trong ride hoặc VScode
...
... ${webDebug} = true \ \ ==> sử dụng trình duyệt đang mở hiện tại
...
... ${webDebug} = false ==> chạy auto như bình thường ở trình duyệt mở với lện CMDh ở trên
SeleniumLibrary.Open browser ${EMPTY} ${testBrowser} options=add_experimental_option("debuggerAddress","127.0.0.1:6969")


Bình luận