Tuesday, January 31, 2017

i Hate iFrames on Selenium C#

On my project i had to face with some elements which is inside a iframe.
So the steps are;

  1. Click on a link (Which opens the iFrame).
  2. Locate some elements inside the iFrame.
  3. Close the iFrame by the close button on the iFrame.
Opening the iframe is Okey, Not a big deal.

This is my Iframe.


Issue happens when locating some elements on it.

I tried with several methods, But nothing works.
Finally i found that i have to switch to the iFrame 1st.


 IWebElement detailFrame = elm.Iframe;
 Driver.SwitchTo().Frame(detailFrame);


Then closing the iFrame.
I tried with pressing the escape key. -But NO luck.

Actions action = new Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape).Build().Perform();

Then tryed on clicking on some other area out of the iFrame.
and it works.

But before that i has to switch back to the default frame.

Actions action = new Actions(Driver);
Driver.SwitchTo().DefaultContent();
action.Click(elm.Summary).Build().Perform();

And im Done.



Wednesday, January 18, 2017

Deploying a selenium test project on clients server/PC

I was able to successfully run all my selenium test cases thru VS test explorer by right clicking on it.
But after some time client ask me to provide the test suite in order to deploy it on there server.(which dont have VS, only MS test framework)

Then i navigate to bin folder of my solution and copy everything on it zip it and send it to client.

Idea is to run the project dll using MSTEST test runner, as below.

set mstestPath="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"

%mstestpath%\mstest /testcontainer:SeleniumTestProject1.dll

But client got this weird error on there server.

Test Run Failed.
  Failed  10
  ---------
  Total   10
Results file:  E:\Users\<user>\documents\visual studio 2012\Projects\TestWithSeleni
um3\TestWithSelenium3\bin\TestResults\<user>_ES-NB-110 2017-01-18 22_11_17.trx
Test Settings: Default Test Settings

Run has the following issue(s):
Warning: Test Run deployment issue: The assembly or module 'WebDriver' directly
or indirectly referenced by the test container 'E:\Users\<user>\documents\visual st
udio 2012\Projects\TestWithSelenium3\TestWithSelenium3\bin\testwithselenium3.dll ' was not found.

Then i tried the same on my PC on a different location and i got the same error.

after googling i found a easy solution.

Just add
 string value = AppDomain.CurrentDomain.BaseDirectory;
to your code (at the start point of your test method) Add a breakpoint to the newly added code and check what is the path of value variable.
continue the test process and after everything get success navigate to the folder of values variable.
You might see all the dlls inside the folder. Just copy them and past ware ever you want and execute the project dll using mstest command line tool.
set mstestPath="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"

%mstestpath%\mstest /testcontainer:CodedUITestProject1.dll

Then i again bundle the everything inside the folder (values folder) and send it to client.



Setting up relative path for the gecko driver on selenium with c#

I have to check what was the currant location which my test is going to execute. Actually i was thinking that is inside the bin folder but i was wrong.
 string value = AppDomain.CurrentDomain.BaseDirectory;
From this you can get the actual location where your test project is building right now.
in my case it is on E:\\Users\\<user>\\documents\\visual studio 2012\\Projects\\TestWithSelenium3\\TestResults\\cja_ES-NB-110 2017-01-18 22_43_22\\Out
Then based on the above location i have to give the relative path. My Gecko driver was inside the TestWithSelenium3 folder so i have to move back 3 time as below.
@"..\..\..\TestWithSelenium3"
Then it works,

FInal test method will looks like :
 [TestMethod]
        public void CodedUITestMethod1()
        {
            var path = Application.CommonAppDataPath;
            string value = AppDomain.CurrentDomain.BaseDirectory;
            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"..\..\..\TestWithSelenium3", "geckodriver.exe");
            IWebDriver driver = new FirefoxDriver(service);
            driver.Navigate().GoToUrl("http://www.google.com");
            Thread.Sleep(4444);
        }

Error message on selenium 3 with firefox

After updating the selenium driver to 3.0 i got few error messages and 1 was with the Gecko driver.
Then i add it to my project as on below and try to run the script.

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"E:\Users\\documents\visual studio 2012\Projects\TestWithSelenium3\TestWithSelenium3", "geckodriver.exe");

            //service.Port = 64444;
            //service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";

IWebDriver driver = new FirefoxDriver(service);

driver.Navigate().GoToUrl("http://www.google.com");

Then i got the below error which is little it different than the 1st error.(im not posting the 1st error because it was for the gecko driver.)

Test method TestWithSelenium3.CodedUITest1.CodedUITestMethod1 threw exception: 
System.InvalidOperationException: Unsupported Marionette protocol version 2, required 3
Result StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxDriverService service)
   at TestWithSelenium3.CodedUITest1.CodedUITestMethod1() in e:\Users\cja\documents\visual studio 2012\Projects\TestWithSelenium3\TestWithSelenium3\CodedUITest1.cs:line 34

I tried so many things and had no luck,
So finally i checked my fire fox version and i was 44.XXXX..

Then i update the firefox to 47.0.2 and it works.
Cheers ...

Friday, September 16, 2016

Using ExtentReports with MSTest framework on C#

When i was dealing with coded UI ,Selenium and Appium test result reporting i found this super cool reporting framework called "ExtentReports".





I love the UI.
And this can be use with MSTest and Nunit framework too.

Below are the steps to how to deal with the ExtentReports on MSTest.

1.     Downloadthe latest ExtentReports DLL, or use this command with NuGet manager.
Install-Package ExtentReports
2.     Add the downloaded file as a reference.
3.     Use the namespace “using RelevantCodes.ExtentReports;” in order to use the newly downloaded dll.
4.     Then on your test project at the class level define below variables;
·         public static ExtentTest test { get; set; }
·         public static ExtentReports extent { get; set; }
·         public TestContext TestContext { get; set; }

5.     Use an assembly initialize attribute and provide the folder for the HTML report.
[AssemblyInitialize]
static public void TestSetUp(TestContext TestContext)
{
extent = new ExtentReports("../../Reports/ExtentScreenshot.html", true);
        }

6.  Inside your test method write down the things or steps that you are performing by the Extended Reports way as on below.

[TestMethod]
        public void Test_OtherTestatNewClass()
        {

            test = extent.StartTest("Test case one");<-Test case name.

            test.Log(LogStatus.Pass, "Start Compairing two values");<-Explain the step
            Assert.AreEqual("123", "1233");
            test.Log(LogStatus.Pass, "End Compairing two values");

            LogStatus status = test.GetCurrentStatus();
            test.Log(LogStatus.Info, "Test Finished");
            test.Log(LogStatus.Info, status.ToString());  
            test.Log(LogStatus.Pass, "Test Passed");
        }

7.  Then use a test cleanup method and it will do the things if the above test get failed.
[TestCleanup]
        public void LogError()
        {
         
            var status = TestContext.CurrentTestOutcome.ToString();
            var status2 = Console.Error.ToString();

            this.TestContext.WriteLine(Console.Error.ToString());

            if (status =="Failed")
            {
                test.Log(LogStatus.Fail);
                test.Log(LogStatus.Fail,"Test Failed");
              

            }
           
            extent.EndTest(test);
        }

8.  Then at last use an assembly cleanup method in order to write everything to the the HTML file

[AssemblyCleanup]
        static public void EndReport()
        {
            extent.Flush();       
            extent.Close();
        }

DONE

Wednesday, June 1, 2016

Trouble shooting error messages on selenium C# -02

I faced below exception :( 

Test method AppiumDTZ.Test_ProfilePage.Validate_Updating_Mobile_Phone_umber threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: unknown error: Element is not clickable at point (180, 546). Other element would receive the click: <div class="bar bar-footer ugl-footer app-common-footer" dtz-hide-on-keyboard-show="">...</div>
  (Session info: webview=33.0.0.0)
  (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.3 x86_64)
Result StackTrace:

Root course for this was, the element which im try to click was under a transparent element.
The transparent layer was static and cant move.

So when ever i try to click on the button it will search on the transparent layer.
There for i have to scroll the bottom layer to the top or the maximum  to get my button out of the transparent layer.

I tried with different mechanisms to scroll the layer but nothing works for me except this.


                IJavaScriptExecutor js = (IJavaScriptExecutor)InitialiseAppium.driver;
                js.ExecuteScript("arguments[0].scrollIntoView()", <elemant name>);

Monday, May 30, 2016

Trouble shooting error messages on selenium c# -01

The CLR has been unable to transition from COM context 0x12aa9d0 to COM context 0x12aab40 for 60 seconds.

Above message is bit weird error message.


This happens to me in  middle of  debugging a selenium code via visual studio 2012.

Solution :
Debug -> Exceptions -> Managed Debug Assistants menu in Visual Studio and uncheck the ContextSwitchDeadlock

reff :http://stackoverflow.com/questions/2747916/the-clr-has-been-unable-to-transition-from-com-context-for-60-seconds