質問

I am using the Watir Splash framework to test a web application, and I have setup two page classes. The first is the "Login" page which is detailed here:

module App
 module Page
 class Login < WatirSplash::Page::Base
  url "http://[removed].com"

    def login_btn
        modify button(:id => 'btnLogin'), :click => lambda {redirect_to VehicleSelection}
    end

The other page class is the "Vehicle Selection" page. I have used the modify method as shown in the documentation here to ensure that the vehicle selection page object is available for RSpec after a successful login.

But what happens if the login failed? I have some test cases that deliberately feed incorrect information into the login form to ensure that the authentication is working properly. RSpec would need the methods defined in the "Login" class to access the correct elements to complete the test case. In this case, the way that I have specified the method a "VehicleSeleciton" object will be returned regardless. (or so it appears)

Any help is appreciated. Also, I'm open to other suggestions for testing frameworks, especially if there is more example code for me to reference.

役に立ちましたか?

解決

Below are a couple of approaches I have tried. I was not using the WatirSplash framework, but the same concepts applied (though the attempted WatirSplash example code might not be 100% accurate).

Solution 1: Do return page objects

My personal preference is to not have page objects returning page objects. Instead, I find it easier to read/work with explicit initializations of each page object within the test. Alister Scott discussed this in his blog.

Your tests would then look like:

#For login successful tests
page = App::Page::Login.new
page.login_btn.click
page = App::Page::VehicleSelection.new  #The VehicleSelection page is explicitly initialized
page.validate_page #or whatever you want to do with the page

#For login failed tests
page = App::Page::Login.new
page.login_btn.click
page.validate_page #or whatever you want to do with the page

Solution 2: Create multiple methods for login

Another solution, would be to create two login methods - one for successful login and one for unsuccessful login.

The page object could be:

module App
    module Page
        class Login < WatirSplash::Page::Base
            url "http://[removed].com"

            def login(user, password)
                #Do whatever code to input name and password and then click the button

                #Then redirect to the VehicleSelection page since that is where you will want to go most often
                redirect_to VehicleSelection
            end

            def login_failed(user, password)
                login(user, password)               

                #Return the Login page (instead of the VehicleSelection page).
                redirect_to Login
            end
        end
    end
end

With the tests being:

#For login successful tests
login_page = App::Page::Login.new
vehicle_page = login_page.login(user, password)
vehicle_page.validate_page #or whatever you want to do with the Vehicle Selection page

#For login failed tests
login_page = App::Page::Login.new
login_page.login_failed(user, password)
login_page.validate_page #or whatever you want to do with the Login page

Solution 3: Make the button know where it is going

Another solution, would be to have the login button know which page to redirect to.

The page object could be:

module App
    module Page
        class Login < WatirSplash::Page::Base
            url "http://[removed].com"

            def login_btn(login_successful=true)
                if login_successful
                    modify button(:id => 'btnLogin'), :click => lambda {redirect_to VehicleSelection}
                else
                    modify button(:id => 'btnLogin'), :click => lambda {redirect_to Login}
                end
            end
        end
    end
end

With the tests being:

#For login successful tests
login_page= App::Page::Login.new
vehicle_page = login_page.login_btn.click
vehicle_page.validate_page #or whatever you want to do with the Vehicle Selection page

#For login failed tests
login_page= App::Page::Login.new
login_page.login_btn(false).click
login_page.validate_page #or whatever you want to do with the Login page

他のヒント

Thanks for trying out my gem WatirSplash. I would have written something in the lines of solution #2 - e.g. create two separate methods for successful login and failed login. Using #modify is not needed in either method, like Justin did.

Also, i'd suggest you to use my other gem test-page instead, which is more or less the same, as Page Objects in WatirSplash, but it is extracted into separate gem - WatirSplash will be deprecated in the long term due to all of its parts being exctracted into separate gems giving better control of which functionality is needed in each project.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top