Watir / MiniTest - 未定义的局部变量或方法'浏览器'

时间:2021-07-04 23:53:42

I have 66 watir scripts that I have been creating over the past week to automate testing on the clients website.

我在过去一周内创建了66个watir脚本,以便在客户端网站上自动化测试。

However I have recently found out about a test framework called MiniTest which I am trying to implement now.

但是我最近发现了一个名为MiniTest的测试框架,我现在正试图实现它。

The reason I have set the URL as a variable is because there are 5 different sites that these tests need to run on so when they want me to run my pack on a different website I just need to update that 1 variable and not in each individual test.

我将URL设置为变量的原因是因为有5个不同的站点需要运行这些测试,因此当他们希望我在不同的网站上运行我的包时,我只需更新该1个变量而不是每个个体测试。

require 'minitest/autorun'
require "watir-webdriver"

class MPTEST < MiniTest::Unit::TestCase

def setup()
    url = "http://thewebsite.com/"
    $browser = Watir::Browser.new :chrome
    $browser.goto url
end

def test_myTestCase
    $browser.link(:text, "Submit your CV").click
    sleep(2)
    $browser.button(:value,"Submit").click
    assert($browser.label.text.includes?("This field is required"))

def teardown
    $browser.close
end

end

When running that I receive the following output:

运行时,我收到以下输出:

NameError: undefined local variable or method 'browser' for #<MPTEST:0x4cc72f8>c:/directory stuff...

Any ideas?

EDIT I have browser working however now there is an issue with my assert:

编辑我有浏览器工作,但现在我的断言存在问题:

New code:

require 'minitest/autorun'
require "watir-webdriver"

class MPTEST < MiniTest::Unit::TestCase

def setup()
    url ="http://thewebsite.com"
    $browser = Watir::Browser.new :chrome
    $browser.goto url
end

def test_myTestCase
    $browser.link(:text, "Submit your CV").click
    sleep(2)
    $browser.button(:value,"Submit").click
    assert($browser.label.text.includes?("This field is required"))
end

def teardown
    $browser.close
end

end

And the error is:

错误是:

NoMEthodError: undefined method 'includes?' for "":String

2 个解决方案

#1


0  

The exception

NoMEthodError: undefined method 'includes?' for "":String

Is due to strings, in this case the value returned by $browser.label.text do not have an includes? method.

是由于字符串,在这种情况下$ browser.label.text返回的值没有包含?方法。

The method you actually want is include? (no plural):

你真正想要的方法包括? (无复数):

assert($browser.label.text.include?("This field is required"))

#2


1  

it seems to me you can you use @browser instead of $browser (but the problem might be not in this code)

在我看来,你可以使用@browser而不是$浏览器(但问题可能不在此代码中)

#1


0  

The exception

NoMEthodError: undefined method 'includes?' for "":String

Is due to strings, in this case the value returned by $browser.label.text do not have an includes? method.

是由于字符串,在这种情况下$ browser.label.text返回的值没有包含?方法。

The method you actually want is include? (no plural):

你真正想要的方法包括? (无复数):

assert($browser.label.text.include?("This field is required"))

#2


1  

it seems to me you can you use @browser instead of $browser (but the problem might be not in this code)

在我看来,你可以使用@browser而不是$浏览器(但问题可能不在此代码中)