Encountering a problem where an error is being thrown when using @driver.quit in the after(:context) block

Currently experimenting with this code snippet

require 'rspec'
require 'selenium-webdriver'

RSpec.describe 'Brand new test' do

  before :each do
    @driver = Selenium::WebDriver.for (:firefox)
  end

  after :all do
    @driver.quit
  end

  it 'will sign up and generate a fresh user' do
    p 'testing'
  end
end

In the course of running this, an error arose

rspec test.rb                                                                                                                     [13:04:18]
"testing"
.
An issue surfaced in an `after(:context)` hook.
Failure/Error: @driver.quit

NoMethodError:
  undefined method `quit' for nil:NilClass
# ./test.rb:11:in `block (2 levels) in <top (required)>'


Completed in 4.61 seconds (files took 0.17828 seconds to load)
1 example, 0 failures

I am certain that I am missing something minor, but my efforts have failed thus far. Any assistance would be welcomed.

Coupled with FF or selenium version mismatch?

The versions in use are FF 45, selenium 2.53, and rspec 3.5.4.

Answer №1

The issue at hand pertains to RSpec functionality. In RSpec, before/after(:all) hooks are limited in their access to instance variables. They can only reach variables defined within their scope or outer scopes, not those specified in the current test being executed. However, after(:each) hooks do have the ability to access instance variables declared in the current test.

before :all do
 @x = 5
end

context "example" do
  before :all do
    @y = 6
  end

  it "test case" do
    @z = 7
    # @x, @y, and @z accessible here
  </div>

  after :each do
   #@x, @y, @z accessible here
  </div>

  after :all do
    # @x, @y accessible here
</div>

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

The Javascript executor in Selenium is delivering a null value

My JavaScript code is causing some issues when executed with Selenium's JavascriptExecutor. Strangely, the code returns null through Selenium but a value in Firefox developer console. function temp(){ var attribute = jQuery(jQuery("[name='q& ...

Facing difficulties in initiating Selenium with PhantomJS/GhostDriver as child processes

I am currently working on a Node script that involves using the child_process module in order to run a Selenium server with PhantomJS's GhostDriver. I have imported the module: Child = require "child_process" This is how I am attempting to start the ...

Incomplete roster of kids using Selenium in Python

Does anyone have a solution for retrieving all the children of an element in HTML? Here is the structure of the html: Check out this image of the html structure I've tried the following code: time.sleep(30) stocks = browser.find_element_by_class_na ...

Locate an element by its TEXT using SeleniumBase

When using SeleniumBase, I have a requirement to click on an element that is identified as shown below: <span class="text"> Contato PF e PJ </span> The specific text inside the element (Contato PF e PJ) needs to be ...

Strategies for managing dynamic IDs in XPath selectors

Exploring an IDE on this website through recording and playback. 1. Type any text in the search box, 2. Click on All Products, then select Books. When recording, the xpath is: /html/body[@id='CDS']/div[@id='navContainer']/div[@id=&a ...

How can I select the latest message in a chat using selenium in Python?

I am utilizing the python selenium library to automate tasks. My goal is to programmatically click on the last message that appears in a chat window. Here is the website I am working with: # -*- coding: utf-8 -*- from selenium import webdriver from sel ...

What is the best way to navigate through multiple websites using Selenium and incorporate user input?

I'm new to Python and have a few questions about Selenium: Basically, what I'm attempting is: - users provide multiple links to different sites - the script opens the first link, closes the browser, and then opens the next link - even if the ...

What sets apart initializing a driver object with WebDriver driver = new FirefoxDriver() from using FirefoxDriver driver = new FirefoxDriver()?

There are different ways to create a driver object. We can use: WebDriver driver = new FirefoxDriver(); or FirefoxDriver driver = new FirefoxDriver(); The FirefoxDriver class implements the WebDriver Interface. Is there any difference between using th ...

Why should curly brackets be used within functions?

Why are curly braces used inside a function if the syntax and code already work well? public void Foo() { string txt = "hello world"; { <---- THIS Assert.AreEqual("hello world", txt); } <---- THIS } EXAMPLE: This code snippet w ...

Running Selenium tests simultaneously in "Parallel" with the help of the 'TestNG' framework

Currently, I am running my Selenium Test cases in parallel using TestNG across different browsers (utilizing Selenium-Grid). The challenge I am facing is executing the same test case in various browsers with different sets of data variations. For example, ...

Navigating a new window in Watir

I am facing an issue while trying to use a simple script with Watir. The goal is to log in to a website using Twitter. browser = Watir::Browser.start 'http://www.12mass.com/' browser.link(:class, 'priceLogin').click sleep 2 browser.ele ...

Extracting JavaScript OnClick button using Selenium

I'm having trouble extracting the email address from the following URL: https://www.iolproperty.co.za/view-property.jsp?PID=2000026825 that is only visible after clicking on the "Show email address" button. However, every time I attempt to click and r ...

Using Python with Selenium to extract text from a webpage

How can I utilize Python Selenium to extract the text ": Sahih al-Bukhari 248"? The code snippet provided does not seem to be working as expected. reference = find_element_by_xpath(".//div[3]/table/tbody/tr[1]/td[2]").text print reference See HTML excer ...

Grails combined with selenium-rc for submitting an AJAX form

I am facing an issue while testing the account creation process. The error message I encountered is as follows: Object [object HTMLIFrameElement] has no method 'getCurrentWindow' com.thoughtworks.selenium.SeleniumException: Object [object HTMLIF ...

Is it possible for Doctest to fail if regular output and exceptions are combined?

Is it possible for doctest to handle a mix of output and exceptions together? Consider the following example: >>> def foo(): ... print 'hello world!' >>> foo() hello world! >>> def bar(): ... raise Exception() ...

Guide to setting up selenium and xvfb on Ubuntu

My objective is to execute my test in Behat using the Mink library with a Selenium driver. I have successfully achieved this on my local development machine. However, when it comes to my server, I need to utilize xvfb for headless testing. After reading t ...

The driver instance that has forked will never cease

Issues arise when attempting to run multiple browser windows in Protractor. The code snippet being tested is as follows: I create a new browser instance to perform certain tests. When input is entered into the original browser, it should not affect the ne ...

Python 3.6 script utilizing Selenium unable to extract user review dates and ratings on Google Play Store

I am currently engaged in a project to develop a web crawler using Selenium along with Python 3.6 within a Jupyter Notebook. The primary objective is to extract reviews, their respective dates, and ratings of an APP. The target webpage under consideratio ...

Is it feasible to modify the names of scenario detail files in Cluecumber?

Current software version: 2.2.0 By default, scenario-detail files are labeled as 'scenario_1', 'scenario_2' etc. Is there a way to automatically rename 'scenario_1.html' to scenarioName.html? I need a fixed URL for each scen ...

Choose Kendo Dropdown using Selenium IDE

I'm having trouble using the Selenium IDE to select an item from a kendo dropdown menu. Does anyone have any tips on how to accomplish this task? I'm also open to suggestions in C#, although I won't be able to test it right away. I've ...