在Ruby中使用浏览器作为GUI [英] use browser as GUI in Ruby

查看:146
本文介绍了在Ruby中使用浏览器作为GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vbscript中,通常使用浏览器(IE)作为GUI。
请参阅下面的示例,它要求输入名称并将其返回给脚本。在Ruby中你有一些像Tcl和Shoes的GUI,但我想知道如何在浏览器中这样做。什么是最简单的Ruby解决方案?所以没有exta gems或package,没有已经运行的服务器..如果需要gem,最好是在Windows中运行而没有问题。

In vbscript it is common to use the browser (IE) as a GUI. See the example below, it asks for a name and returns it to the script. In Ruby you have a few GUI's like Tcl and Shoes but i wonder how to do this in the browser. What is the simplest Ruby solution to do this ? So no exta gems or packages, no server that is allready running.. If a gem is needed, preferably one that works in Windows without problems.

这里的vbscript示例

Here the vbscript sample

Set web = CreateObject("InternetExplorer.Application")
If web Is Nothing Then
  msgbox("Error while loading Internet Explorer")
  Wscript.Quit
Else
  with web
    .Width = 300
    .Height = 175
    .Offline = True
    .AddressBar = False
    .MenuBar = False
    .StatusBar = False
    .Silent = True
    .ToolBar = False
    .Navigate "about:blank"
    .Visible = True
  end with
End If

'Wait for the browser to navigate to nowhere
Do While web.Busy
  Wscript.Sleep 100
Loop

'Wait for a good reference to the browser document
Set doc = Nothing
Do Until Not doc Is Nothing
  Wscript.Sleep 100
  Set doc = web.Document
Loop

'Write the HTML form
doc.Write "Give me a name<br><form><input type=text name=name ><input type=button name=submit id=submit value='OK' onclick='javascript:submit.value=""Done""'></form>"
Set oDoc = web.Document
Do Until oDoc.Forms(0).elements("submit").Value <> "OK"
  Wscript.Sleep 100
  If web Is Nothing or Err.Number <> 0 Then
    msgbox "Window closed"
    Wscript.Quit
  End If
Loop
name = oDoc.Forms(0).elements("name").value
oDoc.close
set oDoc = nothing
web.quit
set web = nothing
Wscript.echo "Hello " & name


推荐答案

您可以使用Watir gem。该gem原本打算用于驱动IE浏览器,但可以满足您的需求。

You could use the Watir gem. The gem was originally intended to drive the IE browser, but would fit your need.

要查看:

1)安装Watir gem

1) Install the Watir gem

2)创建一个test.htm文件,其中包含以下内容:

2) Create a test.htm file with the following:

Give me a name<br>
<form name="myForm" title="myForm">
    <input type="text" id="name" >
    <input id="submit" type="button" value="OK" onclick='document.myForm.submit.value="Done"'>
</form>

3)运行以下watir脚本,它将打开浏览器到您的表单。输入名称后单击[确定],输出名称。请注意,您可能需要根据保存test.htm的位置更改脚本中文件的位置:

3) Run the following watir script, which will open the browser to your form. After you input the name and click [OK], the name is outputted. Note that you may need to change the location of the file in the script depending on where you saved your test.htm:

require 'watir'

b = Watir::IE.new
begin
    b.goto('file:///C:/Documents%20and%20Settings/Setup/Desktop/test.htm')
    begin
        sleep(5)
    end until b.button(:id, 'submit').value != "OK"
    name = b.text_field.value
ensure
    b.close
end
puts name

我认为这表明了做你想做的事情的一般可行性。也可以验证和动态创建表格。

I think this shows the general feasibility of doing what you want. Validation and dynamic creation of the forms would also be possible.

这篇关于在Ruby中使用浏览器作为GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆