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 ...