Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selenium web recorder and QM
#1
2014-11-24. Updated to work with Selenium IDE 2.8.0. Tested WebDriver 2.44, now works with Firefox.

This class allows to automate Internet Explorer, Firefox and Chrome using Selenium WebDriver.
Selenium is a web recorder and API for web page automation, eg for automated testing. Free, open-source.
Selenium downloads: http://docs.seleniumhq.org/download/
Selenium scripts can be recorded/edited with the Selenium IDE or created manually in QM.
To export current script (Test Case) from Selenium IDE to QM, use the small toolbar with QM icon that QM adds to the IDE.
In QM macros the scripts can be executed in 2 ways:
1. Call ISelenium interface functions directly. The QM Selenium toolbar button creates such code if in Selenium IDE menu Options -> Clipboard Format is selected C# Remote Control.
2. Call a user-defined C# function that uses functions of IWebDriver and other Selenium classes/interfaces available in C#. The QM Selenium toolbar button creates such code if in Selenium IDE menu Options -> Clipboard Format is selected C# WebDriver.
You can find the Selenium API reference in the Selenium website or in the help file downloaded with C# language bindings.

Selenium setup:
Create folder for Selenium files. It should be QM folder's subfolder Selenium ($qm$\Selenium); if not, will need to specify it with SetOptions().
Selenium consists of multiple components without a common installer. Download what you need.
From Selenium website you need:
Selenium IDE. Need it only to record web actions, don't need to run Selenium scripts.
Selenium WebDriver bindings for C#. Create folder C# in the Selenium folder ($qm$\Selenium\C#) and add there all files from downloaded folder net35 (WinXP/7) or net40 (Win8/10).
If you'll use IE, download Internet Explorer Driver Server. Add IEDriverServer.exe to the Selenium folder.
If you'll use Chrome, download Chrome browser driver. Add chromedriver.exe to the Selenium folder.
Also you need:
Windows XP SP2 or later.
.NET framework 3.5 or 4.x. Windows 7 and 8 have it. If your Windows is older and the .NET framework is not installed, download .NET 3.5 from Microsoft.
Firefox. Only for Selenium IDE, because it is a Firefox plugin.
Also you may need:
NUnit. Only if you'll use Assert.IsTrue etc in C#. Don't need for ISelenium functions. Install NUnit eg in Program Files, and copy nunit.framework.dll to the Selenium folder.

Selenium problems:
There are no ways to automate an existing browser instance.
You can reuse only the browser instance started by StartChrome() etc without flag 1 in same QM process.
Also it means that if QM is running as admin, browser processes also run as admin. It is not good for security.
Manually closing a Selenium-started browser does not close its driver process and does not delete temporary files (eg a Firefox profile may be 30 MB).
The correct way to close browser - call Quit in the macro. Or use flag 1 with StartChrome() etc, then Quit is called automatically.
Without flag 1, StartChrome() etc correctly closes the browser that was previously started by the same function (StartChrome() etc) without flag 1 in same QM process.
Another correct way - use flag 2 with StartChrome() etc. It shows a driver console window if the browser is Chrome or IE. Let the user close the console after closing its controlled browser.
If using IE, may need to change Protected Mode in IE Options -> Security. Must be the same in all zones.
The IE driver process may crash when an ISelenium function does not find an element.
Selenium-started IE does not respond to user clicks.
On one of my computers did not work correctly with IE if Java is not installed. You can download Java 32-bit from Oracle. Look in Control Panel, maybe you already have it.
Does not support high DPI. Clicks in wrong place. Tested only with IE.
Does not give the browser window handle. If you need it, use win() after StartChrome() etc.
WebDriver 2.42 did not work with Firefox 32 and later. Version 2.44 works.

Examples
Macro WebDriver help
Code:
Copy      Help
;Example with ISelenium functions called from QM macro
#region Selenium, recorded 2014.09.13 08:21
#compile "__WebDriver"
WebDriver x.Init("")
str baseURL="http://www.quickmacros.com"
ISelenium selenium=x.StartChrome(baseURL) ;;StartChrome, StartFirefox or StartIE

selenium.Open("/index.html")
selenium.Click("id=m_forum"); selenium.WaitForPageToLoad("30000")
selenium.Click("link=QM Extensions"); selenium.WaitForPageToLoad("30000")

if(mes("Close browser?" "" "YN?2")='Y') x.Quit
#endregion
Macro WebDriver help
Code:
Copy      Help
;Example with IWebDriver functions called from C# code
#region Selenium, recorded 2014.09.13 08:21
#compile "__WebDriver"
WebDriver x2.Init(__FUNCTION__)
str baseURL2="http://www.quickmacros.com"
x2.StartChrome(baseURL2) ;;StartChrome, StartFirefox or StartIE

x2.x.Run

if(mes("Close browser?" "" "YN?2")='Y') x2.Quit
#endregion


#ret
public void Run(){
driver.Navigate().GoToUrl(baseURL + "/index.html");
driver.FindElement(By.Id("m_forum")).Click();
driver.FindElement(By.LinkText("QM Extensions")).Click();
}


Attached Files
.qml   Selenium.qml (Size: 36 KB / Downloads: 1,280)
#2
Hi Gintaras,
I try to use this function, but I get some issues.

I use the Selenium IDE in firefox and test it, it can success and no errors.
Then I try to use QM to execute it, it always shows "element not found" and fail. I try to use "StartFirefox, StartChrome or StartIE" and different codes (" Clipboard Format is selected C# Remote Control" and "Clipboard Format is selected C# WebDriver") from Selenium IDE, the QM always shows "element iusses" and fail.

Do you have any ideas ? Thanks you in advance.

my codes as below:( I mask my account and password)

Macro Macro2
Code:
Copy      Help
#region Selenium, recorded 2015/2/11 上午 10:38
#compile "__WebDriver"
WebDriver x.Init()
str baseURL="http://bbs.duowan.com"
ISelenium selenium=x.StartFirefox(baseURL) ;;StartFirefox, StartChrome or StartIE

selenium.Open("/forum-779-1.html")
selenium.Click("//button[@onclick='javascript:udblogin();']"); selenium.WaitForPageToLoad("30000");
selenium.Type("css=input.placeholder.E_acct", "xxxxx")
selenium.Type("css=input.placeholder.E_passwd", "xxx")
selenium.Click("id=isRemMe")
selenium.Click("css=#m_commonLogin > div.form_item.form_opra > a.m_button_large.E_login")

if(mes("Close browser?" "" "YN?2")='Y') x.Quit
#endregion
#3
Here fails too. Selenium does not find this element: "css=input.placeholder.E_acct". When running from Selenium IDE, fails too. Selenium IDE can find the element when using "//div[@id='m_commonLogin']/div/span/input", but need to insert pause 30000 before. However the script in QM fails even then.

I would use QM functions instead.
Macro Macro2497
Code:
Copy      Help
run "http://bbs.duowan.com/forum-779-1.html"
1
int w=wait(30 WV win("PS3讨论区 - 多玩游戏论坛 - - Mozilla Firefox" "MozillaWindowClass"))
1
Acc a.FindFF(w "BUTTON" "" "onclick=javascript:udblogin();" 0x1004 30)
a.DoDefaultAction
AutoPassword "user" "password" 2|4 w 30

AutoPassword here is quite slow. This is faster:
Macro Macro2498
Code:
Copy      Help
run "http://bbs.duowan.com/forum-779-1.html"
1
int w=wait(30 WV win("PS3讨论区 - 多玩游戏论坛 - - Mozilla Firefox" "MozillaWindowClass"))
1
Acc a.FindFF(w "BUTTON" "" "onclick=javascript:udblogin();" 0x1004 30)
a.DoDefaultAction
;g1
a.FindFF(w "INPUT" "" "class=placeholder E_acct" 0x1084 30)
err 0.5; goto g1 ;;sometimes error "failed to get object", probably the macro is too fast, therefore we wait 0.5 s and retry
a.Select(1)
key "user" T "password"
#4
Hi Gintaras, thanks,
The code from you is ok, but I fail in last action to login in.

selenium code:

selenium.Click("css=#m_commonLogin > div.form_item.form_opra > a.m_button_large.E_login")

My code is as below, but it can't work.

Code:
Copy      Help
a.FindFF(w "A" "" "class=m_button_large.E_login" 0x1004 30)
a.DoDefaultAction

Do you have any ideas? thanks in advance.
#5
key Y ;;Enter

Or uncheck "as firefox node":

Code:
Copy      Help
;a.FindFF(w "A" "" "class=m_button_large.E_login" 0x1004 3) ;;error "failed to get object"
a.Find(w "LINK" "" "a:class=m_button_large E_login" 0x3094 3)
a.DoDefaultAction
#6
Hi Gintaras,
It can work perfect, thank again.


Forum Jump:


Users browsing this thread: 1 Guest(s)