selenium脚本
上一篇 /
下一篇 2009-08-14 13:47:08
**
*
*/
package login;
import com.thoughtworks.selenium.*;
import junit.framework.TestCase;
import
java.util.regex.Pattern;
import junit.framework.Assert;
/**
* @author cxwu
*
*/
public class TestLogin extends TestCase {
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
private Selenium browsers;
private SeleneseTestBase stb;
protected void setUp() throws Exception {
super.setUp();
browsers=new DefaultSelenium("localhost",4444,"*iexplore","http://192.168.12.180/");
stb = new SeleneseTestBase();
browsers.start();
}
public void popPage(){//显示出要运行的页面
java.awt.Robot robo = null;
try
{
robo = new java.awt.Robot();
robo.keyPress(java.awt.event.KeyEvent.VK_ALT);
robo.keyPress(java.awt.event.KeyEvent.VK_TAB);
robo.keyRelease(java.awt.event.KeyEvent.VK_TAB);
robo.keyRelease(java.awt.event.KeyEvent.VK_ALT);
}
catch(java.awt.AWTException ae)
{
ae.printStackTrace();
}
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
public void testLogin(){
String s,temps;
browsers.open("/");
browsers.selectWindow("");
browsers.windowFocus();
browsers.windowMaximize();
this.popPage();
browsers.type("user", "admin");
browsers.type("pass", "admin");
s=browsers.getAttribute("//IMG[@id='authImg']@src");
temps=s.substring(50);
System.out.println("temps="+temps);
browsers.type("//INPUT[@id='authcode1']", temps);
browsers.click("//INPUT[@id='loginsubmit']");
stb.pause(1000);
browsers.click("//A[text()='通知']");//利用text来确定元素,在页面中显示为#text
stb.pause(10000);
/*browsers.click("//A[@id='1' and text()='设置']");
//browsers.waitForPageToLoad("10000");
stb.pause(10000);
browsers.check("//INPUT[@id='notify_flag']");*/
//browsers.click("//A[@class='controlbutton' and text()[2]='新建']");
browsers.click("//A[@class='controlbutton'][1]");
stb.pause(5000);
browsers.selectWindow(browsers.getAllWindowTitles()[1]);//获取弹出窗口的方法之一,另外就是使用下边的注释先OPENWINDOW,然后在SELECTWINDOW
browsers.type("description", "first");
browsers.type("//DIV[@id='notify_text']/TEXTAREA", "ni hao a");
browsers.click("//BODY/INPUT[1]");
stb.pause(5000);
//browsers.click("//A[@class='controlbutton'][1]");
/*browsers.openWindow("http://192.168.12.180/admin/adm/adm_add_modify.php?field=notify&action=add&init=0&tpl_type=","tester");
stb.pause(5000);
browsers.selectWindow("tester");
browsers.type("description", "first");
browsers.type("//DIV[@id='notify_text']/TEXTAREA", "ni hao a");
browsers.click("//BODY/INPUT[1]");*/
browsers.selectWindow(null);
stb.pause(5000);
//System.out.println("des:="+browsers.getText("//TBODY[2]/TR/TD[@id='field_description']"));
//System.out.println(browsers.isElementPresent("//TD[@id='field_description' and contains(text(),'first')]"));
assertTrue(browsers.isElementPresent("//TD[contains(text(),'first')]"));
//注意contains(text(),'first')和text='first'是不一样的,如果text='first'不行的时候,可以使用
contains(text(),'first')
browsers.click("//TD[@id='field_notify_policy']/A[@class='controlbutton']");
stb.pause(5000);
browsers.selectWindow("newwindow");//通过WINDOW的名字或者是ID,TITLE来选择弹出窗口
//browsers.selectWindow(browsers.getAllWindowTitles()[1]);
//browsers.selectWindow(browsers.getAllWindowNames()[1]);
stb.pause(5000);
browsers.type("//INPUT[@name='end0']","1");
browsers.type("//INPUT[@id='interval0']", "0.01");
browsers.type("//INPUT[@name='end1']","2");
browsers.type("//INPUT[@id='interval1']", "0.01");
browsers.type("//INPUT[@name='end2']","3");
browsers.type("//INPUT[@id='interval2']", "0.01");
browsers.click("//INPUT[@type='button'][1]");
stb.pause(5000);
browsers.selectWindow(null);
browsers.click("//A[text()='通知']");
browsers.click("//A[@class='controlbutton' and @id='view_1']");
stb.pause(1000);
browsers.selectWindow(browsers.getAllWindowNames()[1]);
stb.pause(1000);
//System.out.println(browsers.isElementPresent("//DIV[@id='body' and contains(text(),'ni hao a')]"));
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
收藏
举报
TAG: