3 Web Automation
This section has the following chapters:
- Packaging_an_O2Platform_Script_as_a_stand_alone_tool(in_this_case_the_WatiN_based_‘IE_Script’_tool).md
- Writing_an_IE_Automation_script_to_login_into_UK’s_Wifi_(using_O2_Platform’s_WatiN_ExtensionMethods).md
3.1 Packaging an O2 Platform Script as a stand alone tool (in this case the WatiN based ‘IE Script’ tool)
If you grab the latest version of the O2 Platform and try to run the _**IE Script **_tool
you might get a bunch of compilation errors, like the ones Arnaud described in this How to get a “full” version of o2 mailing list thread.
The best way to deal with this is to run this O2 Script as a ‘packaged script’, i.e. from a stand-alone exe that contains all dependencies required to run it.
The rest of this post shows how to create such stand-alone exe for the IE Script tool.
Open the Package O2 Script tool/script
which looks like this:
Then click on Find an O2 Script:
And Drag-n-drop the IE Automation (Simple mode).h2 into the Drop Zone
_
_![]()
The button should go green to represent an active build/package process
And look like this when completed (the button goes red if there are compilation or packaging errors)
That 3.084kb exe file is now our packaged script :)
You can run this executable directly from here:
or copy it to another vm with .NET 4.0 installed
and run it from there:
In some cases (like this one), there will be two new folders created in the executable folder.
The O2.Platform.Scripts (containing the scripts dynamically compiled by the REPL)
And the O2.Temp (which will contain all temp files (including the O2 assemblies that were embedded in the stand-alone exe and extracted to facilitate the compilation))
Going back into the tool that created the stand alone script, the logs provide really good info on what happened:
and if you open the __BuildFiles_ you can see the VisualStudio project that was programmatically created and compiled
In fact, you can open that IE Automation (Simple mode).csproj file in VisualStudio
And run the tool (or a customized version of it) from there:
Note: I just uploaded the IE Automation (Simple mode) v1.0.exe tool to the O2 Platform downloads at Google Code, so you can also grab it from there:
3.2 Writing an IE Automation script to login into UK’s Wifi (using O2 Platform’s WatiN ExtensionMethods)
Here is an example of how to write an O2 Platform IE Automation script that will login a user into a wifi connection that needs a username and password.
Open the IE Script tool which you can get from this stand-alone version (see Packaging an O2 Platform Script as a stand alone tool (in this case the WatiN based ‘IE Script’ tool) )
Or from the main O2 Platform gui:
When opened it, should look like this:
Leave the first line and open the default wifi connection page (see at the end of this post for the scripts created in a format you can copy and paste):
Take a look at the HTML links of this page (I commented out the ie.open since the browser session is persisted on multiple executions):
Here is how to get a specific link (note the multiple variations caused by the fact that the Get Online link has no ID and a new line in its text:
Next step is to click on the link:
Next get the links for this page and look at the details of the link we want to click next:
Which has the same issue of a new line at the beginning and no ID
Let’s click on that Link:
And look at the fields in this page:
In there find the password one
Note that we can edit this field and see its changes in real-time:
We can now get the reference to the password field:
and change its value programmatically:
Now the email field is going to be a little more complicated since it wasn’t picked up by the _fields WatiN _Extension Method.
So take a look at the **.elements() **:
And get a programmatic reference to it:
Get its outerHtml (at this stage I’m trying to figure out the most efficient way to populate it)
Here are the element attributes:
Btw: taking a look at the parent’s element outerHtml we can see that this input element is not properly terminated:
Ok, here is one way to populate the Email field (by directly changing/replacing the outerHtml)
And since they are using jQuery on this site, we can use also use jQuery to populate that field:
Top tip: you can also get javascript objects into your C# script. For example here is how to get the value of the email we just populated:
The document.location object
The window.screen object
A jQuery selector:
The body html (as seen by jQuery)
ok, moving back to our login page….
Now that we can populate data into both input fields, we need to find the button:
and click on it:
Now that its working let’s package the whole script into a lamdba method:
**Note: **since we have jQuery, we could use it to add an attribute to the link, and then get that link from the C# REPL (instead of doing that lamda search):
Next step is to ask the user for the account details and use it to login:
Now, when you click execute you will get a popup you can use to enter the email and password:
And if all goes good you will be logged in, and google should open up:
Finally, we can make this into a stand alone script:
which will open the IE/WatiN control in a popup window:
and even package it as a stand-alone exe:
which can then be executed directly:
(note that in this case there are no extra folders since the embedded dlls are extracted directly into memory and there are no scripts to dynamically compile)
**
****
****Scripts used in this blog:**
Open web page
1 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false); // ie r\
2 andom value for o2cache makes this object to unique amongst multiple instances of this con\
3 trol
4 ie.open("https://service.thecloud.net/service-platform/");
5
6 return "done";
7
8 //O2File:WatiN_IE_ExtensionMethods.cs
9 //O2Ref:WatiN.Core.1x.dll
10 //O2Tag_DontAddExtraO2Files;
Multiple ways to get the GetOnline link:
1 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
2 //ie.open("https://service.thecloud.net/service-platform/");
3 var getOnlineLink = ie.link(@"
4 Get Online");
5 return getOnlineLink.text();
6
7 //these also work;
8 return ie.links()[2].text();
9 return ie.links().third().text();
10 return ie.links().where((link)=> link.text().contains("Get Online")).first();
11
12
13 //O2File:WatiN_IE_ExtensionMethods.cs
14 //O2Ref:WatiN.Core.1x.dll
15 //O2Tag_DontAddExtraO2Files;
Populating the email field using jQuery:
1 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
2 //ie.open("https://service.thecloud.net/service-platform/");
3 //var getOnlineLink = ie.links().where((link)=> link.text().contains("Get Online")).first(\
4 );
5 //getOnlineLink.click();
6 //ie.link(@"
7 //Free Cloud WiFi").click();
8 ie.eval("$('#username').val('AAAAAanother@email.com')");
9 return ie.getJsVariable("$('#username').val()");
10 return "done";
11
12 //O2File:WatiN_IE_ExtensionMethods.cs
13 //O2Ref:WatiN.Core.1x.dll
14 //O2Tag_DontAddExtraO2Files;
Login script as Lambda method
1 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
2
3 Action<string,string> loginIntoTheCloud =
4 (email, password)=>
5 {
6 ie.open("https://service.thecloud.net/service-platform/");
7 ie.links()
8 .where((link)=> link.text().contains("Get Online")).first().click();
9 ie.links()
10 .where((link)=> link.text().contains("Free Cloud WiFi")).first().click();
11 ie.link(@"").click();
12
13 ie.eval("$('#username').val('{0}')".format(email));
14 ie.field("password").value(password);
15 ie.buttons().first().click();
16 };
17
18 loginIntoTheCloud("another@email.com", "password");
19 return "done";
20 //O2File:WatiN_IE_ExtensionMethods.cs
21 //O2Ref:WatiN.Core.1x.dll
22 //O2Tag_DontAddExtraO2Files;
Adding an ID to an element using jQuery:
1 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
2
3 ie.open("https://service.thecloud.net/service-platform/");
4 ie.eval("$(\"a :contains('Online')\").first().parent().attr('id','myLink')");
5 return ie.link("myLink");
6 **Full script with login and redirect to google:**
7
8
9 var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
10
11 Action<string,string> loginIntoTheCloud =
12 (email, password)=>
13 {
14 ie.open("https://service.thecloud.net/service-platform/");
15 ie.links()
16 .where((link)=> link.text().contains("Get Online")).first().click();
17 ie.links()
18 .where((link)=> link.text().contains("Free Cloud WiFi")).first().click();
19 ie.link(@"").click();
20
21 ie.eval("$('#username').val('{0}')".format(email));
22 ie.field("password").value(password);
23 ie.buttons().first().click();
24 };
25
26 var credentials = ie.askUserForUsernameAndPassword();
27
28 loginIntoTheCloud(credentials.UserName, credentials.Password);
29 ie.waitForComplete();
30 ie.open("http://www.google.com");
31
32 //O2File:WatiN_IE_ExtensionMethods.cs
33 //O2Ref:WatiN.Core.1x.dll
34 //O2Tag_DontAddExtraO2Files;
Final version of the script:
1 //var ie = "ie_aenoN".o2Cache<WatiN_IE>(()=> panel.clear().add_IE()).silent(false);
2 var ie = "Util - Login into the cloud Wifi".popupWindow()
3 .add_IE();
4
5 Action<string,string> loginIntoTheCloud =
6 (email, password)=>
7 {
8 ie.open("https://service.thecloud.net/service-platform/");
9 ie.links()
10 .where((link)=> link.text().contains("Get Online")).first().click();
11 ie.links()
12 .where((link)=> link.text().contains("Free Cloud WiFi")).first().click();
13 ie.link(@"").click();
14
15 ie.eval("$('#username').val('{0}')".format(email));
16 ie.field("password").value(password);
17 ie.buttons().first().click();
18 };
19
20 var credentials = ie.askUserForUsernameAndPassword();
21
22 loginIntoTheCloud(credentials.UserName, credentials.Password);
23 ie.waitForComplete();
24 ie.open("http://www.google.com");
25
26 //O2File:WatiN_IE_ExtensionMethods.cs
27 //O2Ref:WatiN.Core.1x.dll
28 //O2Tag_DontAddExtraO2Files;