Webview on Android: pb with 'contexts'

Hello,
I’m doing automation on real android device with Appium on Mac with Ruby.

I see that’s it’s possible to go in a webview in an app but I don’t sucess to do it.
I’ve read on appium website that I have to do :

Given(/^I switch to webview$/) do
webview = @driver.contexts.last
@driver.switch_to.context(webview)
end
Given(/^I switch out of webview$/) do
@driver.switch_to(@driver.contexts.first)
end

But when I try that, I have “undefined method `contexts’ for Selenium::WebDriver::Driver:0x007fefc17d09c0 (NoMethodError)”

I have the last gem of appium_lib and selenium-webdriver.
I’ve put this in my env.rb
require ‘rubygems’
require ‘watir-webdriver’
require ‘rspec’
require ‘page-object’
require ‘page-object/page_factory’
require ‘require_relative’
require ‘yaml’
require ‘selenium-webdriver’
require ‘appium_lib’

The “classic” fonctions of watir selenium are working (like find_element, click…)
So why “contexts” not ?
Maybe I’m missing something.
I decalre:
capabilities =
{
‘appPackage’=> 'myapp.demo,
‘appActivity’=> ‘.MainActivity’,
‘platformName’ => ‘Android’,
‘deviceName’ => ‘Galaxy S4’,
}
server_url = ‘http://127.0.0.1:4723/wd/hub
@driver = Selenium::WebDriver.for(:remote, desired_capabilities: capabilities, url: server_url)
@browser = Watir::Browser.new @driver
@browser.driver.manage.timeouts.implicit_wait = 30

Thanks !

Here you can find my entire code
require ‘rubygems’
require ‘watir-webdriver’
require ‘rspec’
require ‘page-object’
require ‘page-object/page_factory’
require ‘require_relative’
require ‘yaml’
require ‘selenium-webdriver’
require ‘appium_lib’

capabilities =
{
‘appPackage’=> ‘com.myapp.demo’,
‘appActivity’=> ‘.MainActivity’,
‘platformName’ => ‘Android’,
‘deviceName’ => ‘Galaxy S4’,
}
server_url = ‘http://127.0.0.1:4723/wd/hub
@driver = Selenium::WebDriver.for(:remote, desired_capabilities: capabilities, url: server_url)
@browser = Watir::Browser.new @driver
@browser.driver.manage.timeouts.implicit_wait = 30

@driver.find_element(:class, “android.webkit.WebView”).click()

sleep 2

webview = @driver.contexts.last
@driver.switch_to.context(webview)

1 Like

Any help on this ?
Please I really need to get it work.
Am I missing something in my code ? It fails at “webview = @driver.contexts.last” with ““contexts” function unknow.”
Thanks a lot !

Please internet guys, I’m on it from days. I don’t know how to use the appium_lib gem…
Cna you get why the contexts function in not recognized ? Thanks a lot

You should be using the ruby_lib to create your browser, but it looks like you’re using Watir instead?

I was thinking that using watir was the only way to declare the driver/browser. How do you use appium_lib for declaration ?
Thanks !!

I’ve tried a lot of things like
@driver = Selenium::WebDriver.for(:remote, desired_capabilities: capabilities, url: server_url)
@browser = Watir::Browser.new @driver
But I always have an error.
I’ve tried too android testing on this page :
https://www.omniref.com/ruby/gems/appium_lib/0.24.1
But not ok too.
I’m really stuck at this.
How to get into the webview on a app ? If someone can modify my code to do that it will be grate, thanks.

This series should provide you with all the information you need, getting started with ruby!
http://sauceio.com/index.php/2014/07/appium-bootcamp-get-started-with-appium-testing-chapter-1/

Thanks for the link, I’ve read all the chapters but I still don’t get it.
I’m not using saucelabs, only real device.
And I still don’t get how to declare the appium browser/driver (I don’t even know what should it be). Only Sellenium webdriver and Watir browser is recognized.
So I’m not able to get into the webview.
Can you tell me what to change in mode to make it ok ?
Thanks a lot
My code is:

require ‘rubygems’
require ‘watir-webdriver’
require ‘rspec’
require ‘page-object’
require ‘page-object/page_factory’
require ‘require_relative’
require ‘yaml’
require ‘selenium-webdriver’
require ‘appium_lib’
capabilities =
{
‘appPackage’=> ‘com.myapp.demo’,
‘appActivity’=> ‘.MainActivity’,
‘platformName’ => ‘Android’,
‘deviceName’ => ‘Galaxy S4’,
}
server_url = ‘http://127.0.0.1:4723/wd/hub
@driver = Selenium::WebDriver.for(:remote, desired_capabilities: capabilities, url: server_url)
@browser = Watir::Browser.new @driver
@browser.driver.manage.timeouts.implicit_wait = 30
@driver.find_element(:class, “android.webkit.WebView”).click()
sleep 2

#here my code is not working. contexts function is not recognized.
webview = @driver.contexts.last
@driver.switch_to.context(webview)

Hi Ericzered,

Instead of Selenium remote WebDriver use Appium Driver, it will work :smile:

driver = Appium::Driver.new(caps: capabilities).start_driver

hi @Nikhil_Digrase, I tried your recommendation and it worked. Can I asked why your recommendation worked and not the

webview = @driver.contexts.last
@driver.switch_to.context(webview)

Thanks!