如何在 Ubuntu Linux 上使用 FireWatir 进行基本身份验证?

发布于 2024-08-27 06:59:27 字数 1479 浏览 4 评论 0原文

我正在尝试使用 FireWatir (1.6.5) 来使用 Basic 访问网站 身份验证,我一直无法找到有效的解决方案 Linux 中的火狐浏览器。 FireWatir 1.6.5 是否支持基本身份验证 在 Linux 上?我在网上找了两天了,没找到 直接回答任何地方如何做到这一点。

我发现唯一有用的线程是这个( http: //groups.google.com/group/watir-general/browse_thread/thread/d8ab9a177d282ce4/fc1bf2319fb387d8?lnk=gst&q=basic+authentication#fc1bf2319fb387d8)。

Aedorn Varanis 说:“Angrez 的叉子有解决方案,所以我正在使用它 现在。谢谢安格雷兹,工作完美!”,但他没有提到他做什么 做了让事情正常运转的事情。

最初,我尝试使用以下方法绕过身份验证对话框:

browser.goto('http://admin:[电子邮件受保护]')

但是,这会生成一个“确认”对话框,其中显示:

“您即将登录该网站” 172.20.1.1" 和用户名 “admin”。“[取消,确定]

此对话框会阻塞,并且在单击“确定”之前,转到调用不会返回。

然后我尝试添加:

browser.startClicker(“ok”) browser.goto('http://admin: [电子邮件受保护]')

但这也会生成相同的“确认”对话框。

我使用单元测试 /var/ 测试了 startClicker 功能 lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html 它工作得很好,这让我认为使用 startClicker 方法不是处理确认对话框的正确方法。

其他人找到了一种让基本身份验证工作的方法,或者如何单击 确认对话框中的“确定”?我已经无计可施了...

I'm trying to use FireWatir (1.6.5) to access a site using Basic
Authentication and I've been unable to find a solution that works on
Firefox in Linux. Does FireWatir 1.6.5 support Basic Authentication
on Linux? I've been searching the web for 2 days and can't get a
straight answer anywhere as to how to do this.

The only thread I found that seemed helpful was this one (
http://groups.google.com/group/watir-general/browse_thread/thread/d8ab9a177d282ce4/fc1bf2319fb387d8?lnk=gst&q=basic+authentication#fc1bf2319fb387d8).

Aedorn Varanis says " Angrez's fork had the solution so I'm using that
now. Thanks Angrez, works perfectly!", but he doesn't mention what he
did to get things working.

Initially I tried to bypass the authentication dialog box by using:

browser.goto('http://admin:[email protected]')

However, this generates a "Confirm" dialog which says:

"You are about to log in to the site "172.20.1.1" with the username
"admin"." [Cancel, OK]

This dialog blocks, and the goto call won't return until I click "OK".

Then I tried adding:

browser.startClicker("ok")
browser.goto('http://admin:[email protected]')

But this ALSO generates the same "Confirm" dialog.

I tested out the startClicker functionality using the unit test /var/
lib/gems/1.8/gems/firewatir-1.6.5/unittests/html/JavascriptClick.html
and it worked fine, which makes me think that using the startClicker
method is NOT the correct way to take care of the Confirm dialog.

Anybody else found a way to get Basic Auth to work, or how to click
the OK on the confirm dialog? I'm at my wits end...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的轨迹 2024-09-03 06:59:27

这可能是一个漫长而丑陋的解决方法,也可能违反了 watir 哲学的简单性,但既然你已经无计可施了......

1) Sahi (http://sahi.co.in/) 通过将 401 身份验证对话框转换为常规网页来处理它们。

2) Sahi 的代理需要运行,并且您将浏览器指向使用 Sahi 的代理。

3) 然后您可以导航到您的页面,只需使用 watir/firewatir 将用户名密码输入到转换后的 401 身份验证网页中(就像常规表单一样)。

您会承受代理的额外负载,但 Sahi 的表现相当良好,因此您应该能够使其正常工作。

如果您需要进一步的帮助,您可以在此处或 Sahi 的论坛上发帖。

希望有帮助。
-纳拉扬

This may be a long ugly workaround, and may also violate the simplicity of watir's philosophy, but since you are at your wits end ...

1) Sahi (http://sahi.co.in/) handles 401 authentication dialogs by converting them into regular web pages.

2) Sahi's proxy needs to be running, and you point your browser to use Sahi's proxy.

3) You can then navigate to your page and just use watir/firewatir to enter username password into a converted 401 authentication web page, (like a regular form).

You would incur the extra load of the proxy, but Sahi is fairly well behaved so you should be able to make it work.

You could post here or on Sahi's forums if you need further assistance.

Hope that helps.
-Narayan

冷情 2024-09-03 06:59:27

在 Aedorn Varanis 的帮助下,我已经在 Firefox 上工作了
Linux。

Aedorn 向我发送了一个“登录”方法,该方法发出 jssh 命令
检查“需要身份验证”对话框,如果存在,则填充
在用户名/密码中并提交对话框。

我复制并粘贴了他发给我的内容如下:

您使用的方法如下所示:

def logon(username, password, wait=3)
        jssh_command = "var length = getWindows().length; var win;var found=false; for(var i = 0; i < length; i++) { win = getWindows()[i]; if(win.document.title == \"Authentication Required\") { found = true; break; }} if(found) { var jsdocument = win.document; var dialog = jsdocument.getElementsByTagName(\"dialog\")[0];"
        jssh_command << " jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\";"
        jssh_command << " jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\";"
        jssh_command << " dialog.getButton(\"accept\").click(); }\n"
        sleep(wait)
        $jssh_socket.send(jssh_command,0)
        read_socket()
        wait()
end

然后你可以在它自己的内部调用它
在访问该网站之前线程
登录要求:

Thread.new { 登录(用户,通行证) }
@ff.goto("http://some_url.com")
睡觉 3

增加等待和睡眠时间,如果
该页面需要一段时间才能加载。如果你的
主进程尝试运行,而
命令正在通过 JSSH 发送
插座,它会停顿并坐在那里
永远直到被杀。还有,没有
真正的方法来检测
出现身份验证窗口。那
意味着你需要始终确保它
每次都以同样的方式工作,或者它,
也会引起问题。最后,
方法总是必须在
另一个线程,因为一旦
出现身份验证窗口,它
停止所有其他处理,直到
消失了。除此之外,它还有效。

由此,我能够使用以下方法对 FireWatir::Firefox 类进行子类化:
新的浏览器类支持“credentials=”方法,就像
Celerity::浏览器可以。因此,就像使用 celerity 一样,您可以执行以下操作:

require 'browser'
browser = Browser.new
browser.credentials = 'user:pass'
browser.goto('http://some.basic.auth.url')

这将自动填写基本身份验证对话框并让您登录
该网站。

我已在下面发布了 browser.rb 文件的内容(请注意
适用于 Linux 中的 ruby​​+firewatir 和 jruby+celerity):

ENGINE = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
if ENGINE == 'ruby'
  require 'firewatir'

  class Browser < FireWatir::Firefox

    def initialize(options={})
      super(options)
      @username = nil
      @password = nil
    end

    def credentials=(string)
      username, password = string.split(":")
      if username.nil? or password.nil?
        raise "Invalid credentials: #{string})"
      else
        @username = username
        @password = password
      end
    end

    def goto(url, wait=3)
      if @username.nil? and @password.nil?
        return super(url)
      else
        t = Thread.new { logon(@username, @password, wait) }
        result = super(url)
        t.join
        return result
      end
    end

    private

    def logon(username, password, wait)
      jssh_command = "
        var length = getWindows().length;
        var win;
        var found = false;
        for (var i = 0; i < length; i++) {
          win = getWindows()[i];
          if(win.document.title == \"Authentication Required\") {
            found = true;
            break;
          }
        }
        if (found) {
          var jsdocument = win.document;
          var dialog = jsdocument.getElementsByTagName(\"dialog\")[0];
          jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\";
          jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\";
          dialog.getButton(\"accept\").click();
        }
      \n"
      sleep(wait)
      $jssh_socket.send(jssh_command,0)
      read_socket()
    end
  end
elsif ENGINE == 'jruby'
  require 'celerity'
  class Browser < Celerity::Browser; end
else
  raise "Ruby ENGINE '#{ENGINE}' not supported."
end

With help from Aedorn Varanis I've got things working on Firefox in
Linux.

Aedorn sent me a "logon" method which issues a jssh command that
checks for an "Authentication Required" dialog and if it exists, fills
in the username/password and submits the dialog.

I've copied and pasted what he sent me below:

You use a method that looks like this:

def logon(username, password, wait=3)
        jssh_command = "var length = getWindows().length; var win;var found=false; for(var i = 0; i < length; i++) { win = getWindows()[i]; if(win.document.title == \"Authentication Required\") { found = true; break; }} if(found) { var jsdocument = win.document; var dialog = jsdocument.getElementsByTagName(\"dialog\")[0];"
        jssh_command << " jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\";"
        jssh_command << " jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\";"
        jssh_command << " dialog.getButton(\"accept\").click(); }\n"
        sleep(wait)
        $jssh_socket.send(jssh_command,0)
        read_socket()
        wait()
end

Then you can call it within its own
thread just before going to the site
with the login requirement:

Thread.new { logon(user, pass) }
@ff.goto("http://some_url.com")
sleep 3

Increase the wait and sleep time if
the page takes awhile to load. If your
main process tries to run while the
command is being sent through the JSSH
socket, it will stall and sit there
forever until killed. Also, there's no
real way to detect if the
authentication window comes up. That
means you need to make sure it always
works the same way every time, or it,
too, causes problems. Finally, the
method will always have to be in
another thread, because once the
authentication window comes up, it
stops all other processing until it
goes away. Other than that, it works.

From this, I was able to subclass the FireWatir::Firefox class with a
new Browser class which supports a "credentials=" method just like the
Celerity::Browser does. So, just like using celerity, you can do:

require 'browser'
browser = Browser.new
browser.credentials = 'user:pass'
browser.goto('http://some.basic.auth.url')

This will automatically fill in the Basic Auth dialog and log you into
the site.

I've posted the contents of my browser.rb file below (notice this
works in ruby+firewatir and jruby+celerity in linux):

ENGINE = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
if ENGINE == 'ruby'
  require 'firewatir'

  class Browser < FireWatir::Firefox

    def initialize(options={})
      super(options)
      @username = nil
      @password = nil
    end

    def credentials=(string)
      username, password = string.split(":")
      if username.nil? or password.nil?
        raise "Invalid credentials: #{string})"
      else
        @username = username
        @password = password
      end
    end

    def goto(url, wait=3)
      if @username.nil? and @password.nil?
        return super(url)
      else
        t = Thread.new { logon(@username, @password, wait) }
        result = super(url)
        t.join
        return result
      end
    end

    private

    def logon(username, password, wait)
      jssh_command = "
        var length = getWindows().length;
        var win;
        var found = false;
        for (var i = 0; i < length; i++) {
          win = getWindows()[i];
          if(win.document.title == \"Authentication Required\") {
            found = true;
            break;
          }
        }
        if (found) {
          var jsdocument = win.document;
          var dialog = jsdocument.getElementsByTagName(\"dialog\")[0];
          jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\";
          jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\";
          dialog.getButton(\"accept\").click();
        }
      \n"
      sleep(wait)
      $jssh_socket.send(jssh_command,0)
      read_socket()
    end
  end
elsif ENGINE == 'jruby'
  require 'celerity'
  class Browser < Celerity::Browser; end
else
  raise "Ruby ENGINE '#{ENGINE}' not supported."
end
深海少女心 2024-09-03 06:59:27

我一直在与这个问题作斗争,直到今天。显然我多次忽略了答案,因为它看起来不合理。然而,解决方案在于 Firefox 的“network.http.phishy-userpass-length”配置文件配置。如果 FireWatir 允许您修改您的 Firefox 实例配置文件,那么您可以将“network.http.phishy-userpass-length”的值指定为 255,这将使该对话框消失。检查 http://kb.mozillazine.org/Network.http.phishy-userpass-长度了解更多详细信息。

注意:使用 Ruby 中的 Capybara + selenium-webdriver,我做到了;

    require 'capybara'
    require 'selenium-webdriver'

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['network.http.phishy-userpass-length'] = 255
    Capybara::Selenium::Driver.new(:profile => profile, :browser => :firefox)

I battled long and hard with this issue until today. Apparently i overlooked the answer many times because it didn't look plausible. However, the solution lies in Firefox's "network.http.phishy-userpass-length" profile configuration. If FireWatir allows you to modify your firefox instance Profile, then you can give the "network.http.phishy-userpass-length" a value of 255 which should make that dialog disappear. check http://kb.mozillazine.org/Network.http.phishy-userpass-length for more details.

Note: With Capybara + selenium-webdriver in Ruby, I did;

    require 'capybara'
    require 'selenium-webdriver'

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['network.http.phishy-userpass-length'] = 255
    Capybara::Selenium::Driver.new(:profile => profile, :browser => :firefox)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文