Search code examples
iosrubyappiumappium-ios

Can't figure out the errors in my first Appium ruby test


require 'rubygems'
require 'appium_lib'

desired_caps = {
    caps: {
        platformName:  'iOS',
        platformVersion: '11.4',
        deviceName:    'iPhone 7',
        app:           Path,
        automationName: 'XCUITest',
    },
    appium_lib: {
        sauce_username:   nil,
        sauce_access_key: nil,
        wait: 60
    }
}

driver = Appium::Driver.new(desired_caps)
Appium.promote_appium_methods AppiumWorld
driver.start_driver

#login test class

    #find the email textfield
    def email(driver)
        return driver.find_element(:name, "Email")
    end

    #find the password textfield
    def password(driver)
        return driver.find_element(:name, "Password")
    end

    #find the login button
    def loginButton(driver)
        return driver.find_element(:name, "Login")
    end

    #find Forgot Password button
    def forgotPasswordButton(driver)
        return driver.find_element(:name, "Forgot Password?")
    end

    #find Don't have an account button
    def forgotAccountButton(driver)
        return driver.find_element(:name, "Don't have an account?")
    end

$driver.driver_quit

I haven't included the path but it is correct. I have installed all the gems needed, the only dependency for ruby to run appium ruby test is appium_lib which I have installed. I want to just make sure that I have the 2 required textfields for email and password and 3 button that include the login, forgot password, and the don't have an account button. Here is the error that I am getting, I can't seem to find a solution to this error:

Traceback (most recent call last):
    2: from loginScreenTest.rb:2:in `<main>'
    1: from /usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/local/Cellar/ruby/2.5.1/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- appium_lib (LoadError

)


Solution

  • The purpose of this test was to check that all the UI elements were present on the view controller. In order to find those "elements", those elements need to be referenced using either a name, id, or even xpath (this isn't recommended because it creates flaky tests, based on what the Appium simulator says, not my words). The problem in particular with my code was first I needed to have the gem installed, so I ran that code but the code that eventually worked is the following.

    require 'rubygems'
    require 'appium_lib'
    
    desired_caps = {
        caps: {
            platformName:  'iOS',
            platformVersion: '11.4',
            deviceName:    'iPhone 7',
            app:           PATH,
            automationName: 'XCUITest',
        },
        appium_lib: {
            sauce_username:   nil,
            sauce_access_key: nil,
            wait: 60
        }
    }
    
    @driver = Appium::Driver.new(desired_caps,)
    
    @driver.start_driver
    
    #login test class
    
        #find the email textfield
        def email(driver)
            if driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTextField')
                return driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTextField')
            end
        end
    
        #find the password textfield
        def password(driver)
            if driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField')
                return driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeSecureTextField')
            end
        end
    
        #find the login button
        def loginButton(driver)
           if driver.find_element(:name, "Login")
               return driver.find_element(:name, "Login")
            end
        end
    
        #find Forgot Password button
        def forgotPasswordButton(driver)
            if driver.find_element(:name, "Forgot your password?")
                return driver.find_element(:name, "Forgot your password?")
            end
        end
    
        #find Don't have an account button
        def forgotAccountButton(driver)
            if driver.find_element(:name, "Don't have an account?")
                return driver.find_element(:name, "Don't have an account?")
            end
        end
    
        #remember me button
        def rememberMeButton(driver)
            if driver.find_element(:name, "Remember me")
                return driver.find_element(:name, "Remember me")
            end
        end
    
        #toggle
        def toggle(driver)
            if driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSwitch')
                return driver.find_element(:xpath, '//XCUIElementTypeApplication[@name="DataSiteOne"]/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeSwitch')
            end
        end
    
    
        email(@driver)
        password(@driver)
        loginButton(@driver)
        forgotPasswordButton(@driver)
        forgotAccountButton(@driver)
        rememberMeButton(@driver)
        toggle(@driver)
    
    @driver.driver_quit