Running Appium test for Hybrid mobile application using C#

Hi Tom,

I am trying to run my test for our Hybrid mobile application using C# as programming language in visual studio. I recorded the actions using appium studio and exported in visual studio and modified few things in the script. When appium studio recorded i dont see the script in hybrid mode. I made the change in script by setting the capability “AutoWebview” as true and executed by setting the driver context as “WEBVIEW_1”. I am getting the following error when executing the test. Below is the error snapshot

Also is there a way in appium studio to get the recorded scripts in hybrid mode.

Result StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Appium.AppiumDriver1.Execute(String driverCommandToExecute, Dictionary2 parameters) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath) at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context) at OpenQA.Selenium.By.FindElement(ISearchContext context) at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by) at OpenQA.Selenium.Appium.AppiumDriver1.FindElement(By by)

Result Message: System.InvalidOperationException : no such element (An element could not be located on the page using the given search parameters (XPATH=’//select/option[1]’))

I have pasted the script below

[TestFixture]
public class CRM_LOGIN
{
private string reportDirectory = “reports”;
private string reportFormat = “xml”;
private string testName = “CRM_LOGIN”;
protected AndroidDriver driver = null;

    DesiredCapabilities dc = new DesiredCapabilities();

    [SetUp()]
    public void SetupTest()
    {
        dc.SetCapability("reportDirectory", reportDirectory);
        dc.SetCapability("reportFormat", reportFormat);
        dc.SetCapability("testName", "CRMFunctionTest");
        dc.SetCapability(MobileCapabilityType.Udid, "Moto78");
        dc.SetCapability(MobileCapabilityType.App, "C:\\test.apk");
        dc.SetCapability(AndroidMobileCapabilityType.AppPackage, "test.package");
        dc.SetCapability("noReset", false);
        dc.SetCapability(MobileCapabilityType.AutoWebview, true);
        dc.SetCapability(AndroidMobileCapabilityType.AppActivity, ".MainActivity");
        driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), dc);

    }

    [Test()]
    public void TestCRM_LOGIN()
    {
        driver.Context = "WEBVIEW_1";
        driver.FindElement(By.XPath("xpath=//select/option[1]")).Click();
        driver.FindElement(By.XPath("xpath=//*[@class='android.widget.Button' and ./preceding-sibling::*[@contentDescription='Accept? ']]")).Click();
        
    }

    [TearDown()]
    public void TearDown()
    {
        driver.Quit();
    }
}

}

Can someone please help on this.

Hi

You need to add the instrumentation capability in-order to work with hybrid apps

dc.setCapability("instrumentApp",true)

You can test which contexts are available for you

var contextHandlers1 = driver.GetContextHandles();

Hi Tom,

Somehow i am always getting the context as “NATIVE_APP”. I could not get the context handles for webview. Also can you please let me know how to record the script in hybrid mode,meaning recording the DOM elements as normal web element xpath and id. Currently i am getting recorded as android UI automator.

Did you add the capability ?

dc.setCapability(“instrumentApp”,true)

Yes i have added the entry . For getting context i used the below code

        var contexts = ((IContextAware)driver).Contexts;

Hi Tom,

When i execute the scripts using appium studio i am getting the below error

System.InvalidOperationException : no such element (An element could not be located on the page using the given search parameters (XPATH='//*[@id='setup-wizard']/div/div[2]/span/button[2]/i'))

I could able to change the view to webview. But getting element not found exception.

    private string reportDirectory = "reports";
    private string reportFormat = "xml";
    protected AndroidDriver<AndroidElement> driver = null;

    private const double Timeout = 40;


    DesiredCapabilities dc = new DesiredCapabilities();
   

  
    [SetUp()]
    public void SetupTest()
    {
        
        dc.SetCapability("reportDirectory", reportDirectory);
        dc.SetCapability("reportFormat", reportFormat);
        dc.SetCapability("testName", "CRMFunctionTest");
        dc.SetCapability(MobileCapabilityType.DeviceName, "MotoG");
        dc.SetCapability(MobileCapabilityType.App, "C:\\test.apk");
        dc.SetCapability(AndroidMobileCapabilityType.AppPackage, "test");
        dc.SetCapability("noReset", false);
        dc.SetCapability("instrumentApp", true);
        dc.SetCapability(AndroidMobileCapabilityType.AppActivity, ".MainActivity");
        driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), dc);

    }

    [Test()]
    public void TestCRM_LOGIN()
    {
        driver.Context = ("WEBVIEW_1");
        driver.FindElementByXPath("//*[@id='setup-wizard']/div/div[2]/span/button[2]/i").Click();

Can you please help on this.

Hi

Please do the following steps :

  1. Place a breakpoint in the line
driver.FindElementByXPath("//*[@id='setup-wizard']/div/div[2]/span/button[2]/i").Click();
  1. When the test stops at the breakpoint, open the object spy and search for your element from the XPath filter
    for your XPath
    https://i.imgur.com/vUUnJwm.png

  2. If you see the element in the object spy, but your test failsSend me the result from

var contexts = ((IContextAware)driver).Contexts;