How to use webdrivermanager in robotframework

Hi các bạn!

Dạo này covit, rảnh hơn chút nên viết blog hehe.

Hôm nay chúng ta sẽ nói về webdrivermanager robotframework.

Ở bài testcase đầu tiên trong robotframework, để chạy được testcase đầu tiên, thì chúng ta cần phải check version của trình duyệt cần thực thi testcase (ví dụ như chrome), sau đó cần lên tìm chromedriver tương thích với phiên bản đó, rồi down về....

Thay vì phải làm thủ công như vậy thì khi sử dụng webdrivermanager, chúng ta sẽ không phải down chrome bằng cơm nữa hehe.

lợi ích hay như vậy, nhưng mà khi tìm kiếm về webdrivermanager thì các bạn thấy là nó chỉ dùng cho python thuần. Vậy muốn dùng nó trong robotframework thì phải làm sao?

ok, chúng ta cùng tìm hiểu nhé.

Các bạn sẽ tìm thấy nhiều phiên bản khác nhau, nhưng trong khuôn khổ bài viết này, mình sẽ nói về webdriver_manager (xem thêm ở trang chủ)

1. Cài đặt webdriver-manager như sau

pip install webdriver-manager

2. Sau khi cài xong, mở 1 IDE để code python, ví dụ pycharm chả hạn 

3. Tạo mới 1 class python, ở đây mình đặt tên Mycode.py 

trong đó sử dụng đoạn code sau để download driver mong muốn (có thể là chrome hay firefox).

Trong đoạn code đơn giản này, mình đã if else sao cho nó download driver tương ứng với tên trình duyệt mình truyền vào. Các bạn có thể custom lại theo ý thích nhé hehe.

Giải thích code như sau:

- sau khi thư viện nó tự động download driver theo browser_name tương ứng, chúng ta sẽ return lại path đó, rồi trong robotframework, sử dụng lại path đó trong keyword Open Browser

đọc thêm về keyword này tại trang chủ seleniumlibrary

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager


def get_driver_path_with_browser(browser_name):
    if browser_name.lower() == 'chrome':
        driver_path = ChromeDriverManager().install()
    elif browser_name.lower() == 'firefox':
        driver_path = GeckoDriverManager().install()
    print(driver_path)
    return driver_path

4. copy file python vào thư mục gốc của project robotframework

5. Gọi lại code file python trong robotframework bằng cú pháp 

Library           tên file python.py

cụ thể sẽ như này

Library    Mycode.py

6. sau khi import file python vào ở bước 5. giờ đây khi sử dụng, chỉ cần gọi lại tên method trong file python. chú ý khi gọi lại sẽ không còn dấu _ trong tên method, mà thay vào đó sẽ là dấu cách

Để tưởng minh, có thể để tên file python đằng trước để phân biệt đó là method do mình custom từ python

*** Settings ***
Library           SeleniumLibrary
Library           Mycode.py

*** Test Cases ***
demo webdriver manager using robotframework
    ${driver_path}=    Mycode.Get Driver Path With Browser        Firefox
    Open Browser          https://pypi.org/project/webdriver-manager/         firefox       executable_path=${driver_path} 
    Go to    https://pypi.org/project/webdriver-manager/
    Close Browser

Cơ bản thì cấu trúc thư mục sẽ như thế này



Chạy tetscase và nhìn thành quả thôi haha


Code demo có tại github

Vậy là qua bài này, chúng ta đã tìm hiểu cách gọi lại một method của python trong robotframework, cũng như cách sử dụng webdriver_manager với robotframework.

Hẹn gặp các bạn ở các bài tiếp theo. Có gì thắc mắc hãy để lại bình luận nhé :D

Note: IDE để code robotframework, mình hay dùng https://nokia.github.io/RED/ 





2 comments:

  1. Thank you very much for the publishing this artical.Its really beneficial and resourceful to me as a robot developer. I had one query though, how do you import the python file if it's in a different directory from the test case file.

    ReplyDelete
    Replies
    1. you can using RIDE, it have button choose file reource. quickly, you can using ../path file python

      Delete