Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

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 

[Selenium] What's WebDriver

 What's WebDriver

The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.


Selenium 2.0的主要新功能是WebDriver API的集成。除了解決Selenium-RC API中的一些限制之外,WebDriver旨在提供更簡單,更簡潔的編程接口。 Selenium-WebDriver被開發以更好地支持動態網頁,其中頁面的元素可能改變而不重新加載頁面本身。 WebDriver的目標是提供一個精心設計的面向對象API,為現代高級Web應用程序測試問題提供了更好的支持。


Selenium 1.0 + WebDriver = Selenium 2.0



You must download WebDriver for different Browser. 

您必須下載不同瀏覽器的WebDriver

Download link :  http://www.seleniumhq.org/download/


Monday, 3 April 2017

[Selenium] 2. install IDE - pyCharm

寫Code就要用IDE,這是我第一次接觸python ,目前就選擇的pyCharm,據說是最好的IDE,目前使用還不錯。 如果你沒有偏好的IDE,那先跟我一起使用pyCharm Community Edition。

pyCharm 有付費版本跟免費版本, 而pyCharm Community Edition 是免費版的。

下載點 : https://www.jetbrains.com/pycharm/download/#section=windows

Double-click pycharm-community-yyyy.m.exe file to launch the installer. You computer verifies it, and then opens the first page of the installation wizard:

雙擊pycharm-community-yyyy.m.exe文件以啟動安裝程序:



Click Next
點擊Next (下一步)

On the second page of the wizard, you have to specify whether you wish a previous installation to be uninstalled, and, moreover, silently:

在第二步,您必須指定是否希望卸載先前的安裝:


Click Next 

點擊Next (下一步)

3. On the Choose Install Location screen, specify the destination directory where the product will be installed. The installation wizard suggests a default location. To choose a custom location, click Browse and select the desired location in your file system. When ready, click Next.

3.在“Choose Install Location (選擇安裝位置)”屏幕上,指定要安裝的目標目錄。建議默認位置。如果要選擇自定義位置,請單擊瀏覽並在文件系統中選擇所需的位置。準備好後,單擊下一步。



4. On the Installation Options screen, specify the following options:

4.在“Installation Options(安裝選項)”上,指定以下要安裝的選項:




  • Create Desktop shortcut: if you select this check box, the shortcut to PyCharm Edu will appear on your Desktop. Thus you will be able to launch PyCharm Edu from your Desktop, by double-clicking this shortcut icon. If you leave this check-box unselected (this is what the installer suggests), then the shortcut on the Desktop will not be created, and you will have to launch the application from the Start menu or from your file system only.
  • 創建桌面快捷方式:如果選中此Checkbox(復選框),PyCharm Edu的快捷方式將顯示在桌面上。因此,您可以通過雙擊此快捷方式圖標從桌面啟動PyCharm Edu。如果您未選擇此復選框(這是安裝程序建議),則桌面上的快捷方式將不會被創建,您將不得不從“開始”菜單或僅從您的文件系統啟動該應用程序。

  • If you are working on Windows XP, you can opt to select the check-box Create Quick Launch shortcut.
  • 如果您使用的是Windows XP,則可以選擇“創建快速啟動”快捷方式。
  • Choose Python version: in this section, you have to specify which Python version will be installed for PyCharm Edu. The installer suggests you to choose between Python 2.7 and Python 3.5 (which is pre-installed). If you are not sure which version suits you better, refer to the comparison of versions.
  • 選擇Python版本:您必須指定為PyCharm Edu安裝哪個Python版本。安裝程序建議您選擇Python 2.7和Python 3.5(預先安裝)。如果您不確定哪個版本適合您,請參閱版本的比較。我的課程請安裝Python 3.x版


5. On this page of the installation wizard, you have to specify in which folder of the Start menu the application shortcuts will appear. If you do not enter the folder name, then your Start menu will contain the folder JetBrains.
此頁面上,您必須指定開始菜單的哪個文件夾將顯示應用程序快捷方式。如果您沒有輸入文件夾名稱,則“開始”菜單將包含JetBrains文件夾。

Click Install.
單擊Install (安裝)。



See how the installation works:

看安裝的進度:



6.When the installation is ready, you see the last page of the wizard:
6.安裝準備就緒後,您將看到最後一頁:


Select the checkbox Run PyCharm Community Edition (if you want to launch the product immediately), and click Finish.
選中運行PyCharm Community Edition(如果要立即啟動產品)複選框,然後單擊Finish完成。


If you have had a previous install of PyCharm you can import your previous settings.

如果您以前安裝了PyCharm,您可以導入以前的設置。

PyCharm gives you the option to use a selection of themes, this is down to personal preference, but for those that are planning long coding sessions, Darcula is always easier on the eyes.

PyCharm可以讓您選擇使用主題,這取決於個人喜好,但對於那些長時間編碼的人來說,Darcula是對眼睛比較好的。


Now PyCharm is installed we’re ready to create our first project

現在,PyCharm已經安裝好,我們可以來創建我們的第一個項目


Change the name in the location textbox to HelloWorld and click "Create"

將位置文本框中的名稱更改為HelloWorld,然後單擊“Create

Now that our project has been created we need a Python file in which we can write code. Select File from the menu then New.

現在我們的項目已經創建,我們需要一個Python文件,我們可以在其中編寫代碼。從菜單中選擇 File (文件) ,然後選擇New...(新建)。


Select Python File from the available options
從選項中選擇Python文件

Finally we need to select a name for our Python file, just use Hello
最後,我們需要為我們的Python文件選擇一個名稱,使用Hello

At last we’re ready to write our first Python program and it’s a very simple one.
最後,我們準備編寫我們的第一個Python程序,這是一個非常簡單的程序。

In the new tab that has been opened type print(“hello world”)
在新的頁面,輸入 print(“hello world”)

You will notice that whilst typing print you will see a pop up displaying information about the method, and PyCharm will automatically add the closing bracket, how thoughtful!

你會注意到在敲打時你會看到一個彈出來顯示關於該方法的信息,PyCharm會自動添加關閉括號,設想非常周到!



Now all that’s left to do is run the program, you can do this by selecting run -> run hello from the menu bar, to pressing shift F10.

現在剩下要做的就是運行程序,你可以通過從菜單欄中選擇run  - > run hello,按shift F10來執行。


Note an output windows has appeared at the bottom of the application containing the below text:

注意,輸出窗口出現在包含以下文本的應用程序底部:路徑會依個人安裝和項目的路徑而不同

C:\Programs\Python36\python.exe C:/Kenneth/Projects/Selenium/HelloWorld/HelloWorld.py

hello world

Process finished with exit code 0

So, what does this mean? Well C:\Programs\Python36\python.exe is the executable of the Python interpreter on your PC.

這是什麼意思? C:\ Programs \ Python36 \ python.exe是您的PC上的Python解釋器的可執行文件。

C:/Users/kenneth/PycharmProjects/HelloWorld/Hello.py is the file we have created that has been executed, note the file name HelloWorld.py inside of the HelloWorld project

C:/Users/kenneth/PycharmProjects/HelloWorld/Hello.py是我們已經執行的文件,請注意HelloWorld項目中的文件名HelloWorld.py

hello world  Is the output from the program we have just ran.
hello world 是我們剛剛運行的程序的輸出。

And Process finished with exit code 0 just lets us know that the program ran without any errors.

而Process finished with exit code 0只是讓我們知道程序運行沒有任何錯誤。

You can refer to HellWorld.py on my GitHub.

Congratulations you have written your first Python program!

Leave a comment if you have any questions and follow on the Python programming series as we move onto more advanced and much more interesting areas of the Python language


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()


Selenium: Firefox Profile exception Can't load the profile

I use python to open firefox and redirect to google page but it show empty page and get error. 
This is the Panel constructor:


Traceback (most recent call last):
  File "C:/Users/KennethHu/PycharmProjects/Web_Automation/Hello Selenium.py", line 3, in 
    driver=webdriver.Firefox()
  File "C:\Programs\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 80, in __init__
    self.binary, timeout)
  File "C:\Programs\Python36\lib\site-packages\selenium\webdriver\firefox\extension_connection.py", line 52, in __init__
    self.binary.launch_browser(self.profile, timeout=timeout)
  File "C:\Programs\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
    self._wait_until_connectable(timeout=timeout)
  File "C:\Programs\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_binary.py", line 108, in _wait_until_connectable
    % (self.profile.path))
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\Users\KENNET~1\AppData\Local\Temp\tmp2rghen87 If you specified a log_file in the FirefoxBinary constructor, check it for details.


Solution: 

elenium team fixed in latest version. For almost all environments the fix is:
pip install -U selenium
Unclear at which version it was fixed (apparently r13122), but certainly by 2.26.0 (current at time of update) it is fixed.

You may get Failure like this Failure to observe this step will give you an error 
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

Download following driver into your project folder. 

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/

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...