selenium web automation test

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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class ActionSelenium {
    public WebDriver driver;
    public String windowsHandle;
    
    public void InitDriver() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
        driver = new ChromeDriver();
        //老版本的登陆页面
        //driver.get("http://www.sacon.com/user/newlogin/from/url/");
        driver.get("http://www.sacon.com/");//默认首页
        driver.manage().window().maximize();
    }
    
    public void inputBox() {
//        driver.findElement(By.name("email")).sendKeys("email");
//        
//        driver.findElement(By.name("email")).clear();
//        String s = driver.findElement(By.name("email")).getAttribute("placeholder");
//        System.out.println(s);
        driver.findElement(By.name("email")).sendKeys("email");
        driver.findElement(By.name("password")).sendKeys("password");
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * 单选框
     * 
     */
    public void radioBox() {
        driver.get("https://www.sacon.com/user/setprofile");
        driver.findElement(By.className("pull-right")).click();
        //driver.findElement(By.xpath("//*[@id=\"profile\"]/div[4]/div/label[1]/input")).click();
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"profile\"]/div[4]/div/label//input"));
        System.out.println(elements.size());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(WebElement radio:elements) {
            boolean flag = radio.isSelected();
            if(flag==false) {
                radio.click();
                break;
            }else {
                System.out.println("选中了...");
            }
        }
        
        
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void checkBox() {
        WebElement check = driver.findElement(By.id("auto-signin"));
        System.out.println("是否选中了checkbox?"+check.isSelected());
        System.out.println("checkbox是否有效?"+check.isEnabled());
        //check.clear();
        
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        check.click();
    }
    
    /**
     * 
     * 按钮
     */
    public void button() {
        WebElement login = driver.findElement(By.className("moco-btn-red"));
        System.out.println(login.isEnabled());
        System.out.println(login.getAttribute("value"));
        driver.findElement(By.className("moco-btn-red")).click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
    public void webForm() {
//        driver.get("xxx");
        driver.findElement(By.id("signin-form")).submit();;
    }
    
    /**
     * 
     * 上传文件
     * */
    public void upHeader() {
        
        driver.get("https://www.sacon.com/user/setbindsns");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String jsString = "document.getElementsByClassName('update-avator')[0].style.buttom=0";
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(jsString);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        driver.findElement(By.className("update-avator")).click();
        driver.findElement(By.id("upload")).sendKeys("C:\\Users\\Administrator\\Desktop\\test.jpg");        
        driver.findElement(By.className("js-avator-save")).click();

    }
    
    /**
     * 
     * 下拉框操作
     * */
    public void downSelectBox() {
        driver.get("https://www.sacon.com/user/setprofile");
        driver.findElement(By.className("pull-right")).click();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        WebElement formElement = driver.findElement(By.id("profile"));
        
        
        WebElement job = formElement.findElement(By.id("job"));
        Select downList = new Select(job);
//        downList.selectByIndex(3);
//        downList.selectByValue("6");
//        downList.selectByVisibleText("学生");
        downList.selectByIndex(1);
        List<WebElement> List = downList.getAllSelectedOptions();
        for(WebElement option:List) {
            System.out.println(option.getText());
        }
        System.out.println(downList.isMultiple());
//        downList.deselectByIndex(3);
        System.out.println(downList.getAllSelectedOptions().get(0));
        System.out.println(downList.getFirstSelectedOption().getText());

        
    }
    
    /**
     * 
     * 
     * 鼠标事件
     * */
    public void mouseAction() {
        //WebElement login = driver.findElement(By.id("js-signin-btn"));
        WebElement login = driver.findElement(By.className("menuContent"));
        List<WebElement> item = login.findElements(By.className("item"));
        Actions actions = new Actions(driver);
        //actions.click(login).perform();
        //actions.doubleClick(login).perform();
        //this.sleep(2000);
        //actions.contextClick(login).perform();
        actions.moveToElement(item.get(0)).perform();
        windowsHandle = driver.getWindowHandle();
        driver.findElement(By.linkText("以太坊")).click();
    }
    
    /**
     * iframe 切换
     * */
    public void iframe() {
        driver.get("http://www.sacon.com/wiki/create");
        WebElement iframeElement = driver.findElement(By.id("ueditor_0"));
        driver.switchTo().frame(iframeElement);
        driver.findElement(By.tagName("body")).sendKeys("This is test");
    }
    
    /**
     * 
     * 窗口
     * */
    public void windowHandle() {
        Set<String> handles = driver.getWindowHandles();
        for(String s:handles) {
            if(s.equals(windowsHandle)) {
                continue;
            }
            System.out.println(s);
            driver.switchTo().window(s);            
        }
        driver.findElement(By.linkText("高级")).click();
    }
    
    /**
     * 
     * 等待
     * */
    public void waitforElement() {
        //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("test")));
    }
    
    private void sleep(int i) {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ActionSelenium as = new ActionSelenium();
        as.InitDriver();
        as.checkBox();
        as.waitforElement();
        as.inputBox();
        as.button();
        as.upHeader();
        as.downSelectBox();
        as.mouseAction();
        as.iframe();
        as.windowHandle();
    }

}

 

转载于:https://my.oschina.net/donngchao/blog/3079988

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值