How can I run Fluentlenium Code inside Selenium Webdriver Firefox Driver? - selenium-webdriver

I am having issues trying to get my Fluentlenium code to run inside the WebDriver Firefox Driver. I need Fluentlenium to execute inside the WebDriver Firefox Driver instead of opening it's own browser. I think I need to override this but I am not exactly sure how to do this. Any help would greatly appreciated. Thanks! Here is what I have for code:
WebDriver driver = new FirefoxDriver();
#Test
public void create_a_picklist()
{
// Go to Page
goTo("http://www.google.com");
}
What happens is that it opens two browsers. One is from the Firefox Driver and the other must be the default browser from the goTo from Fluentlenium. I need it to run this code inside the Firefox Driver window and not open it's own window from Fluentlenium.

By default, it launchs a Firefox browser so that's sufficient :
public class Test extends FluentTest {
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}
And nothing more :)

Ok. Looks like I figured it out. Here is what I did to override the browser:
public class Test extends FluentTest {
// Defines the Driver
public WebDriver driver = new FirefoxDriver();
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
return driver;
}
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}

Related

Using driver.quit() in parallel run

I am running test in parallel using parallel="classes". Till now i was closing browser using driver.close() in #AfterClass. Once test is run, window would close.
public class DriverFactory {
public WebDriver getDriver() {
return setupChromeDriver();
}
}
public class Base {
private final ThreadLocal<WebDriver> driver = new ThreadLocal<>();
#BeforeClass
public void initialise() {
driver.set(DriverFactory.getInstance().getDriver());
}
public WebDriver getDriver() {
return driver.get();
}
#AfterClass
public void closeBrowser() {
driver.get().close();
driver.get().remove();
}
}
Recently i realised close() does not end driver session which caused many chromedriver and geckodriverinstances to stay in memory after test suite is run. I had to kill manually using killall chromedriver.
Solution :
I tried with driver.quit() in #AfterClass, which worked fine and killed session nicely but it worked for test which are not running in parallel.
Challenge:
If i use quit() for parallel run, it closes all running windows. If i use it in #AfterSuite, it will work but it would not be ideal to leave all browser windows open until complete test suite is run . I have around 200 tests.
If i use close() in #AfterClass and use quit() in #AfterSuite, it gives no such session exception because driver is closed which is correct.
Chrome version : 83.0.4103.61
Chromedriver version : 83.0.4103.39
Selenium version : 3.141.59
Any help is appreciated here, thanks .

When I run a test suite with ChromeDrive Chrome is opened at every test

I'm trying to use the same code with Appium driver and Java , TestNG but with ChromeDriver I changed the configuration by adding that code :
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
The problem is that any test case the chrome window is opened a new one, even if my tests are in a correct order with Priority (by TestNG).
Is there a way to work in only one window?
WebDriver driver = new ChromeDriver(); is what opens a new browser each time. Move it to #BeforeClass section and use the same instance in all the tests.
You need to move you code snippet in any #Before type of TestNG methods. Lets say your all test cases are in a TestNG class then
Here is how it should look like:
#BeforeClass
public void deSetup(){
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
}
But if this is not the case, what I mean by this, that your test cases are spread across the multiple TestNG classes then the best way is to have a Singleton class to load and initialize the ChromeDriver. Call method of the Singleton class to instantiate the ChromeDriver in any one of method annotated as #BeforeSuite #BeforeTest or #BeforeGroups. And have a reference variable of WebDriver type in evelry test class and assigne the previously initialized ChromeDriver to this reference variable in #BeforeClass method.
In such a way, ChromeDriver will be instantiated only once when any one of #BeforeSuite #BeforeTest or #BeforeGroups method runs and will be available in every TestNG class once #BeforeClass runs.
This way you can work in only one Chrome window.
I have fixed my problem by this way in #BeforeClass:
#BeforeClass
public static void before() {
// check active session
System.out.println("Becore Test Method");
File file = new File("C:/QA/Emna/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
wd = new ChromeDriver();
}
I made an instance like that:
static WebDriver driver ;
Then in every test I put the following :
#Test
public static void run1() {
//my tests using wd
}
#Test
public static void run2() {
//my tests using wd
}

How to run all cucumber functional automated test cases in single browser

I am new to cucumber testing as well as selenium testing.Please help me to run all cucumber test cases in single browser.As for now i am creating new WebDriver object in each cucumber step_def for feature file.
The solution is, Using / passing the same Web Driver object across your step_def. From your Question i assume, you have multiple Step Def files, If the stories are small and related put all of them in a single step_def file and have a single Web driver object. If it is not the case, invoke every step_def with a predefined Driver object that is globally declared in the configuration loader.
For using one browser to run all test cases use singleton design pattern i.e make class with private constructor and define class instance variable with a private access specifier.Create a method in that class and check that class is null or not and if it is null than create a new instance of class and return that instance to calling method.for example i am posting my code.
class OpenBrowserHelp {
private WebDriver driver;
private static OpenBrowserHelp browserHelp;
private OpenBrowserHelp() {
this.driver = new FirefoxDriver()
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public static OpenBrowserHelp getOpenBrowserHelp() {
if (null == browserHelp) {
browserHelp = new OpenBrowserHelp();
}
return browserHelp;
}
WebDriver getDriver() {
return driver
}
void setDriver(WebDriver driver) {
this.driver = driver
}
public void printSingleton() {
System.out.println("Inside print Singleton");
}
Now, where ever you need to create browser instance than use
WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver();

JBehave #BeforeStory webdriver usage

I'm using the JBehave tutorial (Spring) https://github.com/jbehave/jbehave-tutorial with just 1 change, replacing PerStoryWebDriverSteps to PerStoriesWebDriverSteps.
When I try to use webDriver inside #BeforeStory I have this error message:
org.jbehave.web.selenium.DelegatingWebDriverProvider$DelegateWebDriverNotFound: WebDriver has not been found for this thread.
With this step:
public class LifecycleSteps {
private final WebDriverProvider webDriverProvider;
public LifecycleSteps(WebDriverProvider webDriverProvider) {
this.webDriverProvider = webDriverProvider;
}
#BeforeStory
public void test() {
webDriverProvider.get();
}
...
}
I just want to know if it's a bug, a misconfiguration or not possible to use webDriver in #BeforeStory?
In past I used Behat/Mink (PHP BDD) and It was not possible to use webDriver inside #BeforeStory because webDriver was not load. But in Jbehave you can select the webDriver init by using PerXXXXWebDriverSteps. And I thought it was possible to use webDriver in #BeforeStory if webDriver is init in #BeforeStories (using PerStoriesWebDriverSteps)

Alert doesn't close using Selenium WebDriver with Google Chrome.

I have the following Selenium script for opening alert on rediff.com:
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
Thread.sleep(5000);
Alert alert=driver.switchTo().alert();
alert.accept();
}
}
This very same script is working fine in Firefox and IE9, however using Google Chrome after opening the alert, rest of the code is not working. The main thing is that does not shows any exception, error or anything.
Please provide any solution as soon as possible.
Thanks a lot!
Note: If we need to change any setting of browser or any thing please let me know.
Selenium version:Selenium(2) Webdriver
OS:Windows 7
Browser:Chrome
Browser version:26.0.1410.64 m
I'm pretty sure your problem is a very common one, that's why i never advise using Thread.sleep(), since it does not guarantee the code will run only when the Alert shows up, also it may add up time to your tests even when the alert is shown.
The code below should wait only until some alert is display on the page, and i'd advise you using this one Firefox and IE9 aswell.
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
}
Mostly all that is done here, is changing Thread.sleep(), for a condition that actually will only move forward on the code as soon a alert() is present in the page. As soon as someone does, it wil switch to it and accept.
You can find the Javadoc for the whole ExpectedConditions class here.
Unfortunately AlertIsPresent doesn't exist in C# API
http://selenium.googlecode.com/git/docs/api/dotnet/index.html
You can use something like this:
private static bool TryToAcceptAlert(this IWebDriver driver)
{
try
{
var alert = driver.SwitchTo().Alert();
alert.Accept();
return true;
}
catch (Exception)
{
return false;
}
}
public static void AcceptAlert(this IWebDriver driver, int timeOutInSeconds = ElementTimeout)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOutInSeconds)).Until(
delegate { return driver.TryToAcceptAlert(); }
);
}

Resources