How to run Selenium system tests without requiring to open the browser? - testing

I have a test method created using Selenium, something similar to this:
[TestFixture]
public class Test_Google
{
IWebDriver driver;
[SetUp]
public void Setup()
{
driver = new InternetExplorerDriver();
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void TestSearchGoogleForTheAutomatedTester()
{
//Navigate to the site
driver.Navigate().GoToUrl("http://www.google.co.uk");
//Find the Element and create an object so we can use it
IWebElement queryBox = driver.FindElement(By.Name("q"));
//Work with the Element that's on the page
queryBox.SendKeys("The Automated Tester");
queryBox.SendKeys(Keys.ArrowDown);
queryBox.Submit();
//Check that the Title is what we are expecting
Assert.True(driver.Title.IndexOf("The Automated Tester") > -1);
}
}
When the test runs, it opens an IE and carries out its test.
Imagine there are 200 test methods like this spread across multiple test fixtures, which means IE has to be opened and closed many times (as many as test fixtures since 1 browser will be opened per test fixture).
How to run Selenium system tests without requiring to open the browser?
I mean for example I was thinking it might be possible to develop a windows service to run the Selenium tests in the WinForms Web Browser Control, in which case the browser doesn't have to be opened each time and the tests can run automatically and seemlessly. Not sure how to implement this though?
Or is there any other better known way?
Thanks,

No. Selenium is written in JavaScript; it's designed to run in a real browser to test compatibility with a real browser. There are a variety of other tools out there designed to run tests that simulate a browser; you can look into HtmlUnit or Canoo WebTest.
Hope this will help you.

Have you tried XLT?
It doesnt require an open browser at all
http://www.xceptance.com/products/xlt/what-is-xlt.html

You can run all your tests in one browser instance if you like. You just have to pass your webdriver instance to each test. Like having a singelton WebDriver in a static class from where all your testcases can access the WebDriver. Works fine and is usefull if you got a session to keep

driver = new HtmlUnitDriver(true);
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

Related

How Can I set chrome browser to automatically download a pdf using QAF and WebDriverManager

Using a datasheet, I usually pass a browser name into a class I created to select which browser I want to run my tests from. Recently, I've been working on an app in which I need to download a PDF and then verify its contents. I have everything working successfully other than downloading the PDF. With WebDriverManager, it creates a browser profile every time a test runs, and so, I need to update chromeOptions to download PDFs automatically before at the start of the script.
Here is the code that I already have. I just need help with what to put in the prefs for this to work. -
public static void selectBrowser(String strBrowser) {
switch (strBrowser) {
case "Chrome":
String chromePrefs = "{'goog:chromeOptions':{'prefs':{'profile.default_content_settings.popups':0}}}";
ConfigurationManager.getBundle().setProperty("chrome.additional.capabilities", chromePrefs);
TestBaseProvider.instance().get().setDriver("chromeDriver");
Reporter.log("Chrome Browser was set", MessageTypes.Info);
break;
}
}
After researching for hours, finally found that I needed to use the plugins.always_open_pdf_externally preference. Here is the code for anyone who might need it.
Note, the "goog:" before chromeOptions is necessary since I have WebDriverManager enabled. With 3rd party driver managers, we need to put "goog:" before chromeOptions for it to work.
You can simply put it in the application.properties like this -
chrome.additional.capabilities={"goog:chromeOptions":{"args":[--disable-extensions],"prefs":{"plugins.always_open_pdf_externally":true}}}
or you can put it in the code I have up top like this
String chromePrefs = "{'goog:chromeOptions':{'args':[],'prefs':{\"plugins.always_open_pdf_externally\":true}}}";

How to find the driver instance in QAF framework

During execution I want to know which browser currently the script is running and do some actions based on the browser the script is running.
I am using
Option 1:
if (new QAFExtendedWebDriver().getUnderLayingDriver().equals("ChromeDriver")) {
// Do`enter code here` some thing if this is a chrome browser
}
else if (new QAFExtendedWebDriver().getUnderLayingDriver() instanceof FirefoxDriver) {
}
Thsi option is not working during my excution. What is the way to know which browser I am in ?
Below are few examples:
//to get driver name for this thread which provided using driver.name
String drivername = TestBaseProvider.instance().get().getDriverName();
//if running in browser, browser name for this thread, will not be avialable for mobile native or hybrid apps
String browserName =getDriver().getCapabilities().getBrowserName();
//underlying driver class name, which can be remote driver if you are using remote driver
String driverClassname = getDriver().getUnderLayingDriver().getClass().getSimpleName();
you didn't provided details about what you are trying to achieve. If you want to do something when browser opens, you can utilize driver listener.
If you are using bdd and you have platform/browser specific code, instead of using if else you can have separate step implementation for each platform/browser in different package and load step provider package accordingly, for example:
step.provider.pkg=com.exmple.steps.common;com.exmple.steps.chrome

Can WebDriver replace ChromeDriver in order to make Selenium tests work in all browsers

I'm automating my tests using TestNG and Java:
Can WebDriver replace ChromeDriver in order to make our tests work in all browsers such as Chrome, Firefox, Mozilla, Safari, Opera ... ?
How should we configure the browser so as to use the same code for all browsers?
Essentially, you just new to instantiate a different class derived from RemoteWebDriver depending on the browser you are testing.
e.g.
void GetWebDriver(String browserName) {
if (CHROME.equals(browserName))
return new ChromeDriver(capability);
else if (FIREFOX.equals(browserName))
return new FirefoxDriver(capability);
else if (EDGE.equals(browserName))
return new EdgeDriver(capability);
else if (INTERNET_EXPLORER.equals(browserName))
return new InternetExplorerDriver(capability);
else if (OPERA.equals(browserName))
return new OperaDriver(capability);
else if (SAFARI.equals(browserName))
return new SafariDriver(capability);
}
I suggest you look into this githob project: https://github.com/sebarmeli/Selenium2-Java-QuickStart-Archetype
Specifically, the WebDriverFactory.java file.
The easiest way to run your code in different browsers to use Selenium Grid and RemoteWebDriver. You can find the doc on the following link:
https://github.com/SeleniumHQ/selenium/wiki/Grid2

How do I "recycle" a static class in selenium?

I have been trying to write a Selenium project to test against a Salesforce app. After much trial and numerous error messages popping up intermittently in various places, I think I narrowed it down to something on Saleforces' end. It appears to get hung up after the 10th record. When I tested the next 10 it got hung up at the same spot. I am thinking this may be some sort of DoS defense used by Salesforce. I was also thinking that after every 9 records I would "recycle" the web driver. Is this possible, as it is a static object (following the code by a guru on Pluralsite).
static OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
public static IWebDriver driver = new ChromeDriver(#"C:\mydocs\", options);
But if I close the browser using
driver.Close();
or
driver.Quite();
the entire static class gets hosed. I do not use static classes much. Is there a way to "clear" the class?
Why not just write a method that resets it to a new instance?
public static void resetDriver() {
driver.quit();
driver = new ChromeDriver(#"C:\mydocs\", options);
}

How to handle a failed test in JBehave / Thucydides / Selenium?

I'm pretty new to Thucydides / JBehave, but haven't found any solutions posted to this one. This is the first time I've used Thucydides / JBehave, but have used Selenium before.
I have my .story file. The .story file lists 5 scenario's.
Each scenario is implemented in it's own POJO java class.
e.g
public class ManagerBypassLoginSteps
{
#Steps
ManagerSteps managerSteps;
#Given("the manager is not logged in")
public void mangerLogsIn()
{
managerSteps.start();
}
#When("the manager goes to a different page")
public void managerToDashboardPage()
{
managerSteps.goesToDashboardPage();
}
#Then("they should see the login page")
public void managerShouldSeeLoginPage()
{
managerSteps.verifyOnLoginPage();
managerSteps.close();
}
}
The ManagerSteps class extends net.thucydides.core.steps.ScenarioSteps, but is otherwise just a POJO. The ManagerSteps calls the page objects - all very normal stuff and as per the examples.
In the scenario above, the test fails as the code displays an error message instead of returning the user to the log in page - that's fine, the security code doesn't meet the specified requirements (yet) But, after the assert inside the ManagerSteps class fails, the test appears to stop. This means I have a browser instance just sitting there until I close it. When run as a series of tests, this means a broswer in the Selenium grid is now tied up.
I need a way to detect a test failure, to call to the page object to close / quit.
I can't find the equivalent of a #Before or #After that will always run, I could find it I could use the #After to close the page object's webDriver.
I am not using Thucydides to manage the WebDriver lifecycle as I couldn't find any examples of this that did not use the ThucydidesRunner.class.
Do I have to use the ThucydidesRunner.class ? My impression from the Thucydides manual for integrating with JBehave (http://thucydides.info/docs/thucydides/_writing_acceptance_tests_with_jbehave.html) didn't suggest it was required.
How does everyone else handle test failures in this situation ?
The real problem I had was closing the browser, after a test failure.
The best way to manage the WebDriver appear to be to use the Thucydides #ManagedDriver annotation to handle the lifecyle.
I ended up using a very simple JUnit runner like so
public class UserLogin extends ThucydidesJUnitStory
{
#Managed
public WebDriver webdriver;
#ManagedPages(defaultUrl = "http://localhost:8080/dc-bus-main-web")
public Pages pages;
}
When this is started, it maps to the user_login.story file, through JBehave's naming convention, then starts normally.