I have successfully ported other selenium automation into Ranorex, but the biggest issue that I see is your tests are Java, and either JUnit or TestNG as exported by the selenium IDE in Firefox. There is hope here because the syntax of the Java is very similar to the C# code, and JUnit or TestNG makes little difference in the translation. The reason why I suggest you translate the Java selenium tests to C# is so that you can have full featured reporting in Ranorex, and I will explain the full steps required to get your selenium tests working in Ranorex, and this works for selenium tests that are already in C# or Java selenium tests that need to be translated.
In my selenium test I have the following steps recorded in the IDE:
1) go to Ranorex.com
2) click on support to get the drop down to fix on screen
3) click on support center
4) type "remote testing" into the search box
5) click on the second link which is the Ranorex article on Remote Testing with Ranorex
Consider the following Java JUnit web driver export
Code: Select all
@Test
public void test1Junit() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("page-132-0")).click();
driver.findElement(By.linkText("Support Center")).click();
driver.findElement(By.id("gsc-i-id2")).clear();
driver.findElement(By.id("gsc-i-id2")).sendKeys("remote testing");
driver.findElement(By.cssSelector("#___gcse_1 > div.gsc-control-searchbox-only.gsc-control-searchbox-only-en > form.gsc-search-box > table.gsc-search-box > tbody > tr > td.gsc-search-button > input.gsc-search-button")).click();
driver.findElement(By.xpath("//div[@id='___gcse_2']/div/div/div/div[5]/div[2]/div/div/div[2]/div/div/table/tbody/tr/td[2]/div/a/b")).click();
}
Here is the TestNG web driver export in Java:
Code: Select all
@Test
public void test1Testng() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("page-132-0")).click();
driver.findElement(By.linkText("Support Center")).click();
driver.findElement(By.id("gsc-i-id2")).clear();
driver.findElement(By.id("gsc-i-id2")).sendKeys("remote testing");
driver.findElement(By.cssSelector("#___gcse_1 > div.gsc-control-searchbox-only.gsc-control-searchbox-only-en > form.gsc-search-box > table.gsc-search-box > tbody > tr > td.gsc-search-button > input.gsc-search-button")).click();
driver.findElement(By.xpath("//div[@id='___gcse_2']/div/div/div/div[5]/div[2]/div/div/div[2]/div/div/table/tbody/tr/td[2]/div/a/b")).click();
}
Here is the C# web driver export:
Code: Select all
[Test]
public void The1Test() {
driver.Navigate().GoToUrl(baseURL + "/");
driver.FindElement(By.Id("page-132-0")).Click();
driver.FindElement(By.LinkText("Support Center")).Click();
driver.FindElement(By.Id("gsc-i-id2")).Clear();
driver.FindElement(By.Id("gsc-i-id2")).SendKeys("remote testing");
driver.FindElement(By.CssSelector("#___gcse_1 > div.gsc-control-searchbox-only.gsc-control-searchbox-only-en > form.gsc-search-box > table.gsc-search-box > tbody > tr > td.gsc-search-button > input.gsc-search-button")).Click();
driver.FindElement(By.XPath("//div[@id='___gcse_2']/div/div/div/div[5]/div[2]/div/div/div[2]/div/div/table/tbody/tr/td[2]/div/a/b")).Click();
}
Differences:
1 ) "driver" in Java has "get()" method for browsing to URL, but C# this is "driver.Navigate().GoToUrl()"
2 ) "sendKeys()" in Java is "SendKeys()" in C#
3 ) "click()" in Java is "Click()" in C#
4 ) "By.id()" in Java is "By.Id()" in C#
5 ) "By.linkText" in Java is "By.LinkText()" in C#
6 ) "By.cssSelector()" in Java is "By.CssSelector()" in C#
7 ) "By.xpath()" in Java is "By.XPath()" in C#
8 ) "clear()" in Java is "Clear()" in C#
9 ) "findElement()" in Java is "FindElement()" in C#
More briefly:
1) some classes and methods are not the same at all, but easy to translate as most are only used once at the beginning of a selenium recording
2) all Java methods start with
lower case, and the equivalent in C# starts with
Upper Case
3) Simply doing a find and replace in all documents on a backup copy of your selenium class files should make quick work using the 9 steps listed above, and a tiny bit of manual updating of the files.
4) make sure to do a quick read through of your selenium class files to make sure you have not missed anything.
Now that you have translated your Java into C# the steps from here are unified for anyone attempting to include existing selenium tests in a Ranorex solution.
Here are the steps for using a C# selenium test inside a Ranorex test:
User Case:
User creates a selenium web driver recording, and now wants to include the export from Selenium IDE in Firefox in their Ranorex Studio Test Suite.
Prerequisites:
• User has Firefox
• User has Selenium IDE add-on for Firefox
• User has created at least one simple recording in the IDE
• User exports the test case/recording as a C# class file “.cs”
• User has installed Ranorex Studio and has a license
• User has created a test solution in Ranorex Studio
Steps To Create Selenium IDE Export:
1. Open Firefox and Selenium IDE
2. Perform some simple operations like clicking buttons on a website that will create entries in the Selenium IDE
3. Select File > “Export Test Case” > “C# / NUnit / Web Driver”
4. Name the file “SeleniumTest.cs”
Steps to Create Test Solution in Ranorex Studio:
1. Open Ranorex Studio
2. New Test Solution in C# only. VB is out of the scope of this document.
3. Name the test solution “SeleniumTestSolution”
Including the export in the Ranorex Studio Solution:
1. Right click the project name and Add > “Existing Item”
2. Browse to the Selenium IDE exported test class “.cs” file that you created before.
3. Make sure to copy the file to the project and not link the file. Link will work, but leaves the file separate from your solution. Copy creates a new file with the same name and contents, and also leaves the original intact which gives us the ability to roll-back to the original should we desire to do so.
4. You will now see the selenium export test class file appear in the Project View of Ranorex Studio.
Setup to use Selenium Test Class in Test Suite:
1. In the Test Suite view right click and delete the “Recording1” that was automatically created and added to the first “TestCase”
2. Right click “TestCase” and Add > “New Code Module”
3. Name this new code module “ExecuteSeleniumTest”
Add Required Nuget Packages to Your Project
1. Right click the project and go to “Manage Packages”. If Nuget does not load packages try adding Version 2 repository to Ranorex Studio instead of Version 3 repository.
2. Search for and add “NUnit” package to your solution
3. Search for “Selenium”
4. Add both “Selenium WebDriver” and “Selenium WebDriver Support” to your solution
Edits For “ExecuteSeleniumTest” Code Module:
1. Add “using SeleniumTests;” to the top of the document. “SeleniumTests” with the “s” is the namespace that was created for you by the Selenium IDE. This namespace is defined at the top of “SeleniumTest.cs” file that you exported from Selenium IDE in Firefox.
2. Make “void ITestModule.run()” in “ExecuteSeleniumTest” user code module look like the following code snippet
Code: Select all
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
SeleniumTest mySeleniumTest = new SeleniumTest();
mySeleniumTest.SetupTest();
mySeleniumTest.TheSeleniumTest();
mySeleniumTest.TeardownTest();
}
3. Save “ExecuteSeleniumTest”
Test Execution of Included Selenium Test Class:
1. Build your solution
2. Execute the Test Suite
3. If “ExecuteSeleniumTest” user code module executes correctly you should see Firefox open, your test running, and then close. This is a success! Report will lack detailed testing information at this point.
Optionally Add Testing Details to Ranorex Report:
1. Add “using Ranorex;” to the top of included selenium test class file “SeleniumTest.cs”
2. Add “Report.Info(string)” or “Report.Log(ReportLevel.Info, string)” after each step of the selenium test
3. Save the file
4. Run the test again and check the report for this new output