Selenium-基础操作

一、测试代码

@Test

public void test() {

WebDriver driver = new FirefoxDriver();

 

// 打开当前包中的index页面

driver.get("file:///D:/%E8%B5%B5%E6%AC%A2/Selenium/Selenium/src/com/html/index.html");

WaitSeconds(1000);

// 清除用户输入

driver.findElement(By.id("fname")).clear();

WaitSeconds(1000);

// 输入

driver.findElement(By.id("fname")).sendKeys("这是输入的内容");

WaitSeconds(1000);

// 获取元素内容

String text = driver.findElement(By.name("jsh")).getText();// 这种方法是获取元素的文本值

System.out.println("获取元素内容" + text);

WaitSeconds(1000);

// 以下这种方式是获取input的value值

text = driver.findElement(By.id("fname")).getAttribute("value");

System.out.println("value获取元素内容" + text);

WaitSeconds(1000);

// 单击操作

driver.findElement(By.id("idOfButton")).click();

WaitSeconds(1000);

// 通过浏览器记录向后导航

driver.findElement(By.linkText("This is a link")).click();

driver.navigate().back();

WaitSeconds(1000);

// 向前导航

driver.navigate().forward();

// 刷新

driver.navigate().refresh();

// 关闭网页当前页面

driver.close();

// 关闭浏览器,如果有其他选项卡一并关闭

driver.quit();

// 在windows间移动

WaitSeconds(3000);

driver.switchTo().window("MsgWindow");

// 拖拽,首先拖拽的对象要实现拖拽的方法

WaitSeconds(3000);

WebElement source=driver.findElement(By.id("sourceImage"));

WebElement target=driver.findElement(By.id("targetDiv"));

Actions actions=new Actions(driver);

actions.dragAndDrop(source, target).build().perform();

//actions.clickAndHold(source).moveToElement(target).perform();

// 通过标签

driver.findElement(By.tagName("input")).sendKeys("aaaaa");

// 通过linktext

driver.findElement(By.linkText("This is a link")).click();

driver.findElement(By.partialLinkText("This is")).click();

// 处理下拉列表,找到元素之后new一个select对象

WebElement dElement=driver.findElement(By.id("testingDropdown"));

Select dropdownlist=new Select(dElement);

dropdownlist.selectByIndex(3);

WaitSeconds(2000);

dropdownlist.selectByValue("Performance");

WaitSeconds(2000);

dropdownlist.selectByVisibleText("Manual Testing");

WaitSeconds(2000);

dropdownlist.deselectByIndex(2);

// Alert

driver.findElement(By.id("Alert")).click();

WaitSeconds(2000);

driver.switchTo().alert().accept();

WaitSeconds(2000);

driver.findElement(By.id("Confirm")).click();

WaitSeconds(2000);

String txt=driver.switchTo().alert().getText();

System.out.println(txt);

WaitSeconds(2000);

driver.switchTo().alert().dismiss();

// 滚动,需要调用js方法scrollBy(x,y)

JavascriptExecutor javascriptExecutor=(JavascriptExecutor)driver;

javascriptExecutor.executeScript("scrollBy(0,4000)");

}

二、以上测试代码针对的html页面

 

<html>

<head>

<title>简单测试页面</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="src/com/html/bootstrap.min.css">

<script src="src/com/html/jquery-3.3.1.min.js"></script>

<script src="src/com/html/bootstrap.min.js"></script>

<style></style>

</head>

<body style="font-family: cursive;">

<div class="container">

<div class="row">

<div class="col-md-offset-2 col-md-8" style="font-size: 30; margin-top: 40px; ">

用于自动化测试的Web页面示例

</div>

</div>

 

<div class="row">

<div class="col-md-12" style="font-size:20px; margin-top:40px;" name="jsh">

This is sample webpage with dummy elements that will help you in learning selenium automation.

</div>

</div>

<br>

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<b>This is sample text.</b>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p> <b>Link : </b><a href="https://www.yiibai.com/">This is a link</a></p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>TextBox : </b><input id="fname" type="text" name="firstName" value="文本框内容" ><input id="fname2" type="text" name="firstName" value="第二个文本框" ></p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Button : </b><button id="idOfButton" title="Click me!!" type="button" onclick="this.style.background='green';">Submit</button></p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Radio button : </b>

<form action="#">

<input id="male" type="radio" name="gender" value="male"> Male

<input id="female" type="radio" name="gender" value="female"> Female

</form>

</p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Checkbox :</b>

<form action="#">

<input type="checkbox" class="Automation" value="Automation"> Automation Testing

<input type="checkbox" class="Performance" value="Performance"> Performance Testing

</form>

</p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Drop down :</b>

<select id="testingDropdown">

<option id="automation" value="Automation">Automation Testing</option>

<option id="performance" value="Performance">Performance Testing</option>

<option id="manual" value="Manual">Manual Testing</option>

<option id="database" value="Database">Database Testing</option>

</select>

</p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><button id="dblClkBtn" ondblclick="alert('hi, Yiibai Testing');">Double-click to generate alert box</button></p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p><b>Click button to generate Alert box : </b>

<button id="Alert" onclick="alert('hi, Yiibai Testing');">Generate Alert Box</button>

</p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p> <b> Click button to generate Confirm box : </b>

<button id="confirm" onclick="generateConfirmBox()">Generate Confirm Box</button>

</p>

<p id="demo"></p>

</div>

</div>

<br>

 

<div class="row">

<div class="col-md-12" style="font-size:15px;">

<p>Drag and drop example- drag the below image on the textbox</p>

 

<div id="targetDiv" ondrop="drop(event)" ondragover="allowDrop(event)" style="width:400px;height:150px;padding:10px;border:1px solid #aaaaaa;"></div>

<img id="sourceImage" src="https://www.yiibai.com/static/img/logo.png" alt="yiibai" draggable="true" ondragstart="drag(event)" height="120px">

 

</div>

</div>

<br>

</div>

<script>

//window.open('http://www.baidu.com','MsgWindow');

function generateConfirmBox()

{

var x;

var r=confirm("Press a button!");

if (r==true)

{

x="You pressed OK!";

}

else

{

x="You pressed Cancel!";

}

document.getElementById("demo").innerHTML=x;

}

 

function allowDrop(ev)

{

ev.preventDefault();

}

 

function drag(ev)

{

ev.dataTransfer.setData("Text",ev.target.id);

}

 

function drop(ev)

{

ev.preventDefault();

var data=ev.dataTransfer.getData("Text");

ev.target.appendChild(document.getElementById(data));

}

 

</script>

</body>

</html>

 

posted on 2019-04-03 15:11  zh1990  阅读(148)  评论(0编辑  收藏  举报

导航