Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, 4 June 2017

Getting Started with RobotFramework on Windows

http://www.lyyyuna.com/2016/04/09/robotframework-tutorial-a-complete-example/


Robot Framework 是一個通用的自動化測試框架。

Robot Framework 自身和其核心庫都是由 Python 實現的。因此,如果熟悉 Python (或者打算開始熟悉 :)),用其寫自己的關鍵字是個好的選擇。


Getting Started with RobotFramework on Windows

Install python

1) Go to https://www.python.org/ and download Python 2.7.x version as shown below and install it.


2) Open command prompt and run below command.


3) encounter error when installing robotframework

   3.1)  I have installed  python36 and python27 on my PC so it show the below error           
             NameError: name 'execfile' is not defined. 
           Solution :  Go to python27 folder and use the below command to install robotframework. 
                      


  3.2) When run ride.py, it said "wxPython not found."

         solution :  install wxPython 2.8.12.1  download from https://sourceforge.net/projects/wxpython/files/wxPython/2.8.12.1/  

     3.2)  download  "robotframework-ride-1.5.2.1.win-amd64.exe" but it said "No python installation found in the register"

            solution : install robotframework by Pip 
        

4)  after solve the above issue, continue to run below command. 
 



How to us
e Robot Framework with Selenium2Library & RIDE & Command Prompt

First, open cmd prompt and write “ride.py” and then press Enter. Then, you will see RIDE editor.  

  






Friday, 7 April 2017

[Selenium] 4 Selenium Google WebDriver.

In this section,  You will learn how execute test case by Chrome browser and execute browser behavior by selenium.

1.  Execute test case by Chrome
1.1.Create a [BrowersDriver] folder under Python36 folder.
1.2.Download Goolge WebDriver and save into BrowersDriver folder - link1  link2 
   (P.S : You can save it  wherever you want. )



1.3. Create testChrome.py file in pyCharm IDE.

1.4. Entering the below code and execute it.


        from selenium import webdriver

        url="C:\\Programs\\Python36\\BrowersDriver\\chromedriver.exe"        driver=webdriver.Chrome(url)

        driver.get("http://www.google.com")

        # print current url        print(driver.current_url)
        # print current page title        print(driver.title)

        # Close the browser window that the driver has focus of        driver.close()
        # Call Dispose() => Release all resource.        driver.quit()


       In this test case,
          1. go to Google Website
          2. print current URL - https://www.google.com.sg/?gfe_rd=cr&ei=FlbnWJ2QIrHG8AfLz7zQDg&gws_rd=ssl
          3. print current webpage title - Google
          4. close the browser
          5. release webdriver. 


PS: If your chrome just shows blank page, WebDriver you download does not support your Chrome. 
Solution: download Chrome WebDriver which match your chrome version or 
          downgrade your chrome version. 

My Chrome Version: 57.02987.133 (64 bit)
ChromeDriver.exe version is blank. 

2. Browser Behavior 
    You will learn how to manipulate browser behavior - Go, back, forward, refresh.
2.1 Create a python file - TestDriverBehavior.py
2.2 Entering the below code and execute it.
   
    
print
(driver.current_url) # print current page titleprint(driver.title) # go to Yahoo pagedriver.get("http://www.yahoo.com") # print current urlprint(driver.current_url) # print current page titleprint(driver.title) # go back previous pagedriver.back() # print current urlprint(driver.current_url) # print current page titleprint(driver.title) # loads the next URL in the history list as same as window.history.forward() method.driver.forward() # print current urlprint(driver.current_url) # print current page titleprint(driver.title) # refresh current pagedriver.refresh() # Close the browser window that the driver has focus ofdriver.close() # Call Dispose() => Release all resource.driver.quit()



       In this test case,
          1. go to Google Website
          2. print current URL - https://www.google.com.sg/?gfe_rd=cr&ei=FlbnWJ2QIrHG8AfLz7zQDg&gws_rd=ssl
          3. print current webpage title - Google
          5. go to Yahoo website
          6. print current URL - https://sg.yahoo.com/?p=us
          7. print current webpage title -Yahoo
          8. go back privous web page (Google)
          9. print current URL - https://www.google.com.sg/?gfe_rd=cr&ei=FlbnWJ2QIrHG8AfLz7zQDg&gws_rd=ssl
          10. print current webpage title - Google
          11. go forward to next web page (Yahoo)
          12. print current URL - - https://sg.yahoo.com/?p=us
               13. print current webpage title - Yahoo
          14. refresh current page
          15. close the browser
          16. release webdriver. 
You can download my code on my github 

Wednesday, 29 March 2017

[Selenium] 3 selenium installation using pip

Selenium installation using pip for Windows users


  1. Start a command prompt using the cmd.exe program and run the pip command as given below to install selenium.
    啟動命令提示符 cmd.exe 程序並運行 pip 命令來安裝 selenium.
    C:\Python35\Scripts\pip.exe install selenium
     
Now you can run your test scripts using Python. For example, if you have created a Selenium based script and saved it inside C:\my_selenium_script.py,(If you don't have file, you can download it from my github.- simpleSeleniumTest.py) you can run it like this:

現在您可以使用Python運行測試腳本。例如,如果您創建了基於Selenium的腳本並將其保存在 C:\my_selenium_script.py,(如果沒有,可以從我的github下載- simpleSeleniumTest.py)  則可以像這樣運行: : 
C:\Python35\python.exe C:\my_selenium_script.py


Monday, 20 March 2017

Selenium WebDriver - Maximize window (浏览器窗口最大化)

Selenium WebDriver with python - Maximize window

FireFox : 

         self.driver = webdriver.Firefox()
      self.driver.maximize_window()

Chrome : 
   url = "C:\\Programs\\Python36\\BrowersDriver\\chromedriver.exe"
      options = webdriver.ChromeOptions()
      options.add_argument("--start-maximized")
      self.driver = webdriver.Chrome(url,chrome_options=options)

Fancytree Drag and drop of Node

I want to move a node from right tree to left tree. At begin, I try to implement this action by drag_and_drop function. But failed.


Solution : I use click_and_hold, move_to_element, click and release methods to completed this action.

python:
     righttree = driver.find_element_by_xpath(".//*[@id='right']/ul/li[1]/span/span[3]")

     target = driver.find_element_by_xpath(".//*[@id='left']/ul/li[1]/span")

     actions = ActionChains(driver)
     actions.click_and_hold(righttree)
     actions.move_to_element(target)
     actions.click(target)
     actions.release()
     actions.perform()
     WebDriverWait(driver, 10).until(
       (EC.element_to_be_clickable((By.XPATH, ".//*[@id='left']/ul/li[1]/ul/li/span/span[3]"))))
time.sleep(2)









Tuesday, 14 March 2017

學習自動化測試日記


2017/2/17 :
                   之前就很想學自動化測試,但是一直沒有好的機會學,市面上的書都寫得很深不然就是說個大概而已。 這次在udemy上看到 Seleniun 課程,決定今年要學好,就買下了
!!


 
2017/3/3 :
                 今天上完課了!! 但是發現學了很基本的概念,也發現要學的東西更多,多了一個Robot Framework 。
                寫一個Login測試腳本,遇到問題1. 輸入帳號與密碼登入後要進入下一個並輸入專案名稱,程式跑太快還沒到登入完畢就進入下一步故報Eorre, 解決方案: WebDriverWait (解決)。問題2: 等一次 ,要再等第二次時發現之前的code無法用 (解決)。 問題3: 無法click button which is in iframe.

2017/3//6 :
               上周找問題時看到 Selenium的學習網站 ( http://selenium-python.readthedocs.io/getting-started.html ), 今天發點時間來看,但是看到2.3 Using Selenium to write tests 看到 unitest (pyhton unit testing framework ),看起來很複雜,而且這個是似乎針對 python 的單元測試,目前我的專案是C# ,等學完後要切換至C#學selenium,現在學python因為網路資料多且也想趁機學python。

2017/3/7 :
                今天學Robot Framework,Robot Framework是跑測試並產生出一個報告

2017/3/15
                上週蠻忙得,不過都有寫一點點的測試,最大問題是有時firefox or chrome無法正常執行,例如driver有問題、跳出來是blank page、timeout等等問題。發現當寫測試時要把瀏覽器停止自動更新,因為更新後selenium的driver也許無法搭配使用。昨天就花一個下午解決firefox無法完整執行測試程式,執行firefox後就一直是blank page,爬很多文,最後解決問題是去升級selenium到3.3.1版本。

google key word: "why-blank-page-opens-in-firefox-when-run-with-selenium-for-the-first-time"
          
本來想試這個解決方法的 沒想到升級的解決了我的問題
https://coderwall.com/p/-h-oxa/run-selenium-with-latest-firefox-versions

解決了就繼續往下走,目標是產生測試報告。

2017/3/29
               1. 用 HTMLTestRunner 可以跑出report 但是在unittest下是無法產生的, 一直找不出辦法,也貼在stack overflow 貼文了 還是沒有回應,看來要自己解決了。 http://stackoverflow.com/questions/42945583/python3-htmltestrunner-not-generating-report
          2. 開始寫自動化測試教學。


目前測試環境是
python36
selenium 3.3.1
firefox 48.0.2
chrome Version 56.0.2924.87

2017/3/20 :
            上一周寫一個測試程式,但是是要做drag and drop的測試,一直都弄不出來,問題是我一直執著在用click_and_hold ,當初想是selenium的執行動作太快了所以無法完成,一直想有沒有辦法讓它慢下來,今天改用  click_and_hold 來完成。Yeah!!  繼續往下走


        actions = ActionChains(driver) 
        actions.click_and_hold(righttree)
        actions.move_to_element(target) 
        actions.click(target) 
        actions.release() 
        actions.perform()


https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html

學習路徑 :  1. install python
                    2. install selenium
                    3. write the first testing script -open browser, find elements on page and input value
                    4. install Robot Framework
                 
          2017/3/6 :
                             unitest :  https://docs.python.org/2/library/unittest.html
                              中文網站 : http://www.webdriver.org/nav2/ - 這是已Java當主機的網站
          2017/3/15
                            how to get selenium to wait for page load after a click
                            http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html


     

Tuesday, 7 March 2017

[Selenium] How to check if a checkbox is selected in Selenium WebDriver

我試過is_Enable() 但是都得到是true,所以現在用get_attribute來確認


I have tried is_Enable method but I always get true.
I decide to use "get_attribute" but its return is true/None. not false. Be carefull

[updated] I found answer. I should use is_selected() rather than is_enabled()


is_enabled()
Returns whether the element is enabled.
is_selected()
Returns whether the element is selected.


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
driver.get("your.site.with.dragndrop.functionality.com")
selement = driver.find_element_by_id("widgetID")
print(element.get_attribute("checked"))  //true
element.click()
print(element.get_attribute("checked"))  //None
element.click()
print(element.get_attribute("checked"))  //true

Thursday, 2 March 2017

[Selenium]how to run your test in Chrome


[Selenium]how to run your test in Chrome 



1. Download following driver 

Chrome:https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox:https://github.com/mozilla/geckodriver/releases
Safari:https://webkit.org/blog/6900/webdriver-support-in-safari-10/


2. Unzip driver and put it into a folder. 
      
3. Load your driver in your code. 
    I want to open Chrome browser so I set the path up in my code. 

       
   from selenium import webdriver
   url="C:\\Programs\\Python36\\BrowersDriver\\chromedriver.exe"
   driver=webdriver.Chrome(url)
   driver.get("http://www.yahoo.com")
   driver.close()
   driver.quit()

how to install geckodriver on a windows system


how to install geckodriver on a windows system

Answer: You can put it anywhere. 


1. Download following driver 

Chrome:https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox:https://github.com/mozilla/geckodriver/releases
Safari:https://webkit.org/blog/6900/webdriver-support-in-safari-10/


2. Unzip driver and put it into a folder. 
      
3. Load your driver in your code. 
    I want to open Chrome browser so I set the path up in my code. 

       
   from selenium import webdriver
   url="C:\\Programs\\Python36\\BrowersDriver\\chromedriver.exe"
   driver=webdriver.Chrome(url)
   driver.get("http://www.yahoo.com")
   driver.close()
   driver.quit()

how-to-recursively-create-subfolder-in-each-folder-of-a-directory-in-cmd

test.cmd: @echo off setlocal for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do ( rem enter the directory pushd %%a echo...