UI Automation
This article is the last one in 2013. In the rails project we use capybara to do UI testing. Like click a button/link or fill the form. But i will show how to use capybara without rails in UI automation today.
Sometimes we have to repeat trivial works and these require user to interactive with browser. For instance, upload file to server or input necessary data.
Install below gems and Firefox
1 2 |
|
In orer to use capybara without rails. You need to require capybara
manually.
In default capybara will launch rack server, change run_server
to false
.
Because we assume your server is running. Use selenium
as driver
.
1 2 3 4 5 6 7 8 |
|
With capybara DSL, you can operate UI easily.
Below code shows fill id and password field with admin
then click login button.
1 2 3 4 5 6 |
|
In case you have to wait specified element show in page or element class changed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
How to attach file? In capybara official document, you can use attach_file
to attach file easily.
But i found it doesn’t work in HTML5 input file tag with multiple like
1
|
|
The capybara always show can’t find the element.
1
|
|
Then i think i could click the button then select the file myself.
1 2 3 4 5 |
|
But here is the problem, selenium driver can’t control the native file selection window.
There’s no way to select files in file selection window.
After few days survey, i solved it with rautomation
.
1
|
|
After the file selection window popups, the running program is stoped.
It means select_file
method won’t be executed.
I think it’s because the native window popups and require user to interactive.
So i change capybara click method to selenium method and select_file
can be executed.
1 2 3 4 5 6 7 8 9 10 |
|
The next step is how to select files in native window. The first step is find the native window. I use a simple way to do it. Compare the window number before click upload button and after.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
If the candidate length equals 1, that candidate will be the file selection window.
There should be better way to find the native window.
I try to find window by window’s title but it won’t work. Maybe it’s because my OS or Firebox is Chinese.
After you get the file_select_window
, set the file path to text_field and click the button.
1 2 3 4 5 6 |
|