第一章sketchup教程我们主要是讨论文本,变量,常量和数组。在第三章sketchup教程我们会讨论SketchUp的模型,但是在我们必须要学习这些基础知识,在会跑之前肯定是要学会走路的,对吧!
1.1 Ruby控制台窗口
首先,我们在SketchUp的窗口菜单中选择Ruby控制台选项。Ruby控制台允许我们输入和执行一行代码。或者使用Sketchup Ruby代码编辑器
来写代码执行,不过先熟悉控制台是比较好的选择,在本章的最后我们也会讨论如何编辑多行命令的。
Ruby控制台的使用是非常简单的,在文本框中输入代码然后按下回车就会自动执行代码。显示结果出现在命令上面,如下图:
我们在文本框中输入了下面的命令:
2 + 2
按下回车键,将会出现结果
这是一个有效的Ruby命令,他相当于一个算术表达式。
1.2 数值和数值运算符
在我们使用SketchUp建模时,其中一个常用做的工作就是使用点来画线条和表面。每个点都是由3个数值坐标组成。所以理解Ruby如何捕获数值是非常重要的,我们这节主要讨论数值形式,数值运算符和常见操作。
#断言def assert_true(actual, expect)
expect(actual).to eq(expect)
end
def assert_false(actual, expect)
expect(actual).not_to eq(expect)
end
def assert_include(actual, expect)
expect(actual).to include(expect)
end
def assert_not_include(actual, expect)
expect(actual).not_to include(expect)
end
#关闭当前的tab
Driver.close
#获取浏览器的tab数
Driver.window_handles.length
#根据location来点击元素
Driver.action.move_to_location(location_x, location_y).click.perform
#页面scroll up
Driver.execute_script('window.scrollTo(0,0))
#页面scroll down
Driver.execute_script('window.scrollTo(0,document.body.scrollHeight))
#scroll到特定的元素
Driver.execute_script('arguments[0].scrollintoView(), element)
#获取当前的page的url
Driver.current_url
#获取当前浏览器的title
Driver.title
# switch to特定的frame
Driver.switch_to.frame(target)
#返回到原来的frame
Driver.switch_to.default_content
#删除所有的cookies
Driver.manage.delete_all_cookies
#浏览器返回上一页
Driver.navigate.back
# switch to一个新的tab页:
def switch_to_new_window
current_handle = Driver.window_handle rescue nilDriver.window_handles.each do |handle|next if current_handle == handleDriver.switch_to.window(handle)end
end
#在浏览器创建新的tab页,并且输入URL
current_window_handle =Driver.window handle
before_window_handles =Driver.window handles
Driver.execute_script('window.open())
switch_to_new_window
Driver.navigate.to(new_url)
#浏览器页面刷新
Driver.navigate.refresh
#获取页面元素的,X,Y坐标值
target = Driver.find_element(:id,‘elemeny_id_01')
target.rect.y
target.rect.x
#获取元素的个数
target.size
#获取元素的css
target.css_value(attri)
#获取元素的attribute.target.attribute(attri)