在Ruby中使用浏览器作为GUI

pet*_*ter 1 ruby vbscript user-interface internet-explorer

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

这里是vbscript示例

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
Run Code Online (Sandbox Code Playgroud)

Jus*_* Ko 5

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

查看:

1)安装Watir gem

2)使用以下命令创建test.htm文件:

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>
Run Code Online (Sandbox Code Playgroud)

3)运行以下watir脚本,它将打开浏览器到您的表单.输入名称后单击[确定],输出名称.请注意,您可能需要根据保存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
Run Code Online (Sandbox Code Playgroud)

我认为这显示了做你想做的事情的一般可行性.还可以验证和动态创建表格.