package web.selenium2;
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
* Automated testing: basic functions of page operations
* @author Administrator
*/
public class FirstCase extends TestCase {
//declare a chrome driver object
WebDriver driver = null;
// Execute before each test method is executed
public void setUp() throws Exception{
System.out.println("init......");
//Inject the test driver of the current browser into webdriver.chrome.driver
System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/Desktop/python/chromedriver.exe");
driver = new ChromeDriver();
}
// Execute after each test method ends
public void tearDown() throws Exception{
System.out.println("destory......");
//close the browser
driver.quit();
}
//Test Baidu login
public void testBaiduSearch() {
//Specify the position of the window
//Point p = new Point(300, 300);
//driver.manage().window().setPosition(p);
// Develop baidu
driver.get("http://www.baidu.com");
//Inject the background script into the foreground execution
String js ="alert(\"hello,this is a alert!\")";
//firefox has no Object[] parameter
//((JavascriptExecutor) driver).executeScript(js,new Object[]{});
//Wait for the alert window popped up on the page to close, otherwise the subsequent code execution window events do not match and the test fails
/*try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}*/
// locate the search box
WebElement searchInput = driver.findElement(By.id("kw"));
//Enter keywords in the search box
//CharSequence c= "12";//Cannot be created by new
CharSequence cs[] = { "python"};
searchInput.sendKeys(cs);
//Locate the search button
WebElement searchButton = driver.findElement(By.id("su"));
//click the search button
searchButton.click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
//close the browser
driver.quit();
}
//Test ITeye login
public void testITeyeLogin() {
try{
driver.manage().window().maximize();//The window is maximized
//Open iteye
driver.get("http://zengshaotao.iteye.com/login");
System.out.println(driver.getTitle());
// locate the search box
WebElement usernameInput = driver.findElement(By.id("user_name"));
WebElement pwdInput = driver.findElement(By.id("password"));
CharSequence username[] = { "zengshaotao"};
CharSequence password[] = { "******"};
//Enter username and password on the page
usernameInput.sendKeys(username);
pwdInput.sendKeys(password);
//Locate the login button
WebElement searchButton = driver.findElement(By.id("button"));
//searchButton.getText();
//click the login button
searchButton.click();
//Here is the check of the page content just after login
//The automatic operation code needs to correspond to the specific page scene, that is, here is the page scene after the login is successful, and the page content before the login cannot be located
//Otherwise, an error message will appear because the location cannot be located
WebElement loginResult = driver.findElement(By.id("user_visits"));
//Locate the user_visits object from the login result page
String cssValue = loginResult.getAttribute("class");
if("clearfix".equals(cssValue)){
System.out.println("Login successful...");
}else{
System.out.println("Login failed...");
}
//Return true if the element is displayed, otherwise return false
if(loginResult.isDisplayed()){
System.out.println("The element has been displayed...");
}
//Enter the search content in the search box through the class attribute, and click the search icon
driver.findElement(By.cssSelector(".search_text")).sendKeys(new CharSequence[]{"java"});
driver.findElement(By.cssSelector(".submit_search")).click();
//whether the element is selected
//WebElement checkbox = driver.findElement(By.id("checkbox_id"));
//checkbox.isSelected();//There will be a return value, if checked. Returns true, false if not checked.
//whether the element is enabled
//WebElement enableEle = driver.findElement(By.id("loginBtn"));
//enableEle.isEnabled();
//Submit the content of the form
//<button class="btn btn-major" id="loginBtn" type="submit">登录</button>
//WebElement submitEle = driver.findElement(By.id("submitBtn"));
//submitEle.submit();
//If the element is in an iframe, you need to cut into the iframe first and then locate the specific element
//driver.switchTo().frame("id");//The id of the iframe is passed in
//driver.switchTo().defaultContent();//Switch to the default iframe
//Operate the drop-down box select
/*WebElement element_province = driver.findElement(By.id("province"));
Select province = new Select(element_province);
province.selectByIndex(0); //Select the drop-down box value according to the position of the selected value, starting from 0
province.selectByValue("22");//Select the value of the drop-down box according to the value
province .selectByVisibleText("Beijing");//Operate the drop-down menu according to the visible text*/
}catch(Exception e){
e.printStackTrace ();
}
}
//Simulate the movement of the actual mouse to perform automated testing
public void testAction(){
driver.get("https://www.baidu.com/");
By inputText = By.id("kw");
By searchButton = By.id("su");
//Instantiate the action object
Actions action = new Actions(driver);
CharSequence contents[] = { "java"};
/ / Enter java keywords into the input box through the action simulation keyboard, only use the perform method to enter it
action.sendKeys(driver.findElement(inputText), contents).perform();
//Mouse simulation to move to the search button
action.moveToElement(driver.findElement(searchButton)).perform();
//Simulate click operation
action.click().perform();
}
}