Using ThreadLocal<RemoteWebDriver>, the FluentWait feature in Selenium allows for seamless synchronization between the test

I am currently seeking a solution to incorporate FluentWaits into my Java Selenium test. The issue I am facing stems from using ThreadLocal to declare my drivers as thread-local in order to run them concurrently.

Below is the code snippet in question:

//Variable declaration
protected ThreadLocal<RemoteWebDriver> threadDriverFirefox = null;

//Setting up driver for each thread in BeforeTest method
threadDriverFirefox = new ThreadLocal<RemoteWebDriver>();
threadDriverFirefox.set(new RemoteWebDriver(new URL(urlnode), DesiredCapabilities.firefox()));

//Method to retrieve the driver
 public WebDriver driverFirefox() {
return threadDriverFirefox.get();
}

//Using the driver in the test
driverFirefox().get(weburl);

The main issue lies with the driverFirefox() method. I am struggling to implement it within the FluentWait structure:

Wait waitfluent = new FluentWait(driverFirefox()).withTimeout(30, TimeUnit.SECONDS)
    .pollingEvery(2, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

WebElement testElement = waitfluent.until(new Function() {
    public WebElement apply(WebDriver driverFirefox() ) {
    return WebDriver driverFirefox().findElement(By.id("logEmailField"));
    }
});

However, there seems to be a syntax error...

Multiple markers at this line - Syntax error on token ")", delete this token - Syntax error on token(s), misplaced construct(s)

Any suggestions on how to rectify this situation?

Thank you

Answer №1

To prevent memory leaks, it is important to use a static ThreadLocal variable. This ensures that the reference is shared properly without causing any issues. If the ThreadLocal is static, make sure to access it in the correct way. Remember not to initialize the reference by a thread, but only set the value using the set() method.

public class TestEnvironment {

  static ThreadLocal<WebDriver> CURRENT_DRIVER = new ThreadLocal<>();

  public static WebDriver getCurrentDriver(){
    return CURRENT_DRIVER.get();
  }

   public void setupTest(RemoteWebDriver driver) {
    CURRENT_DRIVER.set(driver);
   }
}

You can now easily define a fluent wait using the shared driver:

Wait fluentWait = new FluentWait(TestContext.getCurrentDriver())
                      .withTimeout(30, TimeUnit.SECONDS)
                      .pollingEvery(2, TimeUnit.SECONDS)
                      .ignoring(NoSuchElementException.class);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python Selenium - failing to insert dynamically generated data into the DOM

I've been working with Python Selenium and have encountered a small issue. Here's the code I'm using: driver = webdriver.PhantomJS() #Alternatively, you can use driver = webdriver.Chrome() driver.get("my_url") driver.find_element_by_xpath(" ...

Error: The function 'fetch' is not recognized in Selenium Console

Having some trouble with Selenium and Chrome Developer Tools. I want to open Selenium, go to a URL, and then use driver.execute_script to make a fetch request via Console in Chrome Developer Tools within the Selenium window. However, when I try to run thi ...

Mastering the art of splitting data into various types with a Fluent API

As part of a project involving Selenium UI testing, I am utilizing the Fluent API. A valuable resource I have come across is an article on Fluent API by Scott Lilly, which explains how to create a smooth interface. The primary goal behind using Fluent API ...

Extracting information from dynamically generated tables using Python 2.7, Beautiful Soup, and Selenium

I am in need of assistance with scraping a JavaScript generated table and saving specific data to a csv file. The tools available to me are limited to python 2.7, Beautiful Soup, and/or Selenium. Although I have referred to the code provided in question 14 ...

Is it possible to webscrape a jTable that contains hidden columns?

I'm currently working on setting up a Python web scraper for this webpage: specifically targeting the 'team-players jTable' I've successfully scraped the visible table using BeautifulSoup and selenium, but I'm facing difficulties ...

Issue with accessing data from database in my app

I am currently working on a project that involves retrieving data from my database. The user inputs a car registration number and a rating (ranging from 1 to 5) and then clicks a button. Upon clicking the button, my code is supposed to execute, fetching te ...

Failure to trigger the success action in ExtJS

I am currently utilizing struts2 for the server-side implementation combined with ExtJS4 for the user interface. I have a basic form that I submit to the server, but the response consistently goes to the failure case of the request even though I am just re ...

Using Selenium Webdriver with Python for navigating through classified ads on leboncoin, encountering difficulty in selecting a dropdown list and input box

Continuing my journey on the Python/Selenium learning curve. Selenium Webdriver - Python - leboncoin - Issue with selecting a button containing an accent After successfully logging in to the site and selecting the button, I aim to populate the fields for ...

The chromedriver.exe application encountered an error while attempting to open the URL "http://www.example.com/" using the driver.Navigate().GoToUrl method

When using navigate.GoToUrl("http://www.example.com/"), the ChromeDriver.exe stops working, but functions properly when the FirefoxDriver is used: using (IWebDriver driver = new ChromeDriver(DRIVER_PATH)) { // driver.Manage().Timeouts().ImplicitlyWait ...

What are the signs that indicate a webpage is offline or unresponsive?

I have developed a Python code that checks the availability of web pages stored in a tuple by browsing them using Selenium. The objective is to regularly check if these pages are online and responding. Here is a snippet of the code: from selenium import w ...

Issue with Selenium Testing: Struggling to choose a date from the calendar options

For some reason, I am facing an issue where I can't seem to select the date value from the calendar list. However, all the other tests are working perfectly fine. import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa ...

Issue arises when attempting to access parameter value from testng.xml while running test class as 'TestNG Test'

My approach to retrieving the browser type (on which I want to run Selenium tests) from testng.xml involves the following code: public class TestClass { @BeforeClass public void beforeClass(ITestContext context) { String browser = con ...

Waiting for Elements to be Added to Parent in a Lazy Loading Website with Selenium and Python

When working with Selenium's expected conditions for wait, such as those mentioned here: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions, I find myself unsure about which one to use or if that is e ...

Utilize BeautifulSoup and Selenium to extract dynamically generated table data from div elements and store it in a list

I am working on scraping table data using Selenium and passing it to Beautiful Soup. The current script I have pulls all text data, but it ends up as one big element in a list. Is there a way for Beautiful Soup to filter by the "table-container" div class ...

No search results appear when the search button is pressed in Python using Selenium

After successfully automating Edge (and Chrome) to navigate to the correct page for searching broadband prices and entering a postcode into the search box, a problem arises when clicking the search button - it fails to display the results. The goal is to o ...

Locator for finding compound text within a div class using Selenium WebDriver

I am struggling to select a specific button in the UI that shares similarities with other elements. Below is the code snippet for the button in question: <div class="ui green ok inverted button"> <i class="checkmark icon"></i> Yes </d ...

Selenium - incapability to select the subsequent page

I'm experiencing difficulties with clicking the next button (>) and repeating this process until reaching the last page. Despite researching similar issues, I have been unable to identify what's causing my code to malfunction. Below is the co ...

Tips for choosing an element created by JavaScript code

I need help selecting the Wireless button on a pop-up window that appears within the same window. There are no iFrames generated and I'm unable to locate any elements inside this Wifi-Connect window. Any suggestions? Here is the code I've attem ...

Help! How can I prevent using Thread.sleep() multiple times in a selenium test?

Currently, I am facing an issue while writing some Selenium tests. My code includes Thread.sleep after every function call in a method, making it repetitive and messy. I want to find a more optimal solution to replace these repeating Thread.sleep calls. ...

The error message "Pyinstaller cannot find the module 'pyfiglet.fonts'" is commonly encountered when using Pyinstaller

Hello, I'm attempting to export my Python Selenium project using PyInstaller. However, every time I try it, the process is successful but when I launch the .exe file, I encounter this error: ModuleNotFoundError: no module named 'pyfiglet.fonts&a ...