在 Ruby 中使用浏览器作为 GUI [英] use browser as GUI in Ruby
问题描述
在 vbscript 中,通常将浏览器 (IE) 用作 GUI.请参见下面的示例,它要求输入名称并将其返回给脚本.在 Ruby 中,您有一些 GUI,例如 Tcl 和 Shoes,但我想知道如何在浏览器中执行此操作.最简单的 Ruby 解决方案是什么?所以没有额外的 gems 或包,没有已经运行的服务器.. 如果需要 gem,最好是在 Windows 中运行没有问题的 gem.
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屋!