Recently, I started playing a web game called ogame. After playing it for a few days, I found that infrastructure is too time-consuming. It will take nearly two hours to upgrade to lev12. I browsed the FAQ and discussion area of the game and found that the game did not provide a public interface, so I wanted to write a program to simplify the operation. I initially considered two solutions:
A simulates IE to help me do some simple operations |
B uses the IE interface to directly trigger the IE event under specific circumstances |
|
The requirements are as follows:
No |
Description |
Priority |
1 |
Regularly refresh the page to obtain the latest information |
High |
2 |
Based on the obtained information, once the resources meet the conditions, the building will be upgraded according to the specified strategy. |
High |
3 |
Mail Notification of latest information |
Low |
4 |
Automatic battle information log and automatic star map information log |
Low |
|
|
|
Prepare to use Ruby for development. First, check the feasibility of Solution A. Use SOCKET to simulate IE to access the website and parse the http package. The workload is heavy.
The B solution has no clue yet. You can only remember an Excel example when accessing the COM interface in ruby.
So Google found that watir (web application testing in ruby) is an open-source framework used to automatically test web applications.
The principle is to use the COM interface of IE to manipulate some behaviors of IE. Exactly what I need.
First, I pulled out a watir sample and accessed Google. It was very simple. Then I ran it.
1 require 'watir'
2 # The watir Controller
3 # Open the IE browser
4ie = watir: IE. New
5 # Step 1: Go to the test site: http://www.google.com
6ie. Goto ("http://www.google.com ")
7 # Step 2: Enter 'pickaxe' in the search text field
8ie. text_field (: name, "Q"). Set ("Pickaxe") # Q is the name of the search field
9 # Step 3: Click the 'Google search' button
10ie. Button (: name, "btng"). Click # "btng" is the name of the Search button
11 # actual result: Check that the 'programming Ruby 'link appears on the results page
12If ie. contains_text ("programming ruby ")
13 puts "test passed. Found the test string: 'Programming Ruby '. Actual results match expected results ."
14 else
15 puts "test failed! Cocould not find: 'Programming Ruby '"
16end
17 # end of test: Google Search
The eclipse console shows:
Test passed. Found the test string: 'Programming Ruby '. Actual results match expected results.
Okay. It starts very smoothly. Let's take a look at how watir works.