Skip to content

Latest commit

 

History

History
60 lines (48 loc) · 1.92 KB

css.py.md

File metadata and controls

60 lines (48 loc) · 1.92 KB

获取测试对象的css属性

场景

当你的测试用例纠结细枝末节的时候,你就需要通过判断元素的css属性来验证你的操作是否达到了预期的效果。比如你可以通过判断页面上的标题字号以字体来验证页面的显示是否符合预期。当然,这个是强烈不推荐的。因为页面上最不稳定的就是css了,css变动频繁,而且通过属性也不能直观的判断页面的显示效果,还不如让人为的去看一眼,大问题一望即知。

代码

下面的代码演示了如何获取测试对象的css属性。

css.html

	<html>
		<head>
			<meta http-equiv="content-type" content="text/html;charset=utf-8" />
			<title>attribute</title>		
			<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
			<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />		
			<script type="text/javascript">
				$(document).ready(function(){
					$('#tooltip').tooltip({"placement": "right"});
				});
			</script>
		</head>
			
		<body>
			<h3>attribute</h3>
			<div class="row-fluid">
				<div class="span6">		
					<a id="tooltip" href="#" data-toggle="tooltip" title="watir-webdriver better than selenium-webdriver">hover to see tooltip</a>
				</div>		
			</div>		
		</body>
		<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
	</html>

css.py

# -*- coding: utf-8 -*- 
from selenium import webdriver
from time import sleep
import os
if 'HTTP_PROXY'in os.environ: del os.environ['HTTP_PROXY']

dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('css.html')

dr.get(file_path)

link = dr.find_element_by_id('tooltip')
print link.value_of_css_property('color')

print dr.find_element_by_tag_name('h3').value_of_css_property('font')

dr.quit()