zoukankan      html  css  js  c++  java
  • webdriverAPI2

     
    public class TestApiTwo {
        WebDriver driver;
        String url1="http://www.xxx.com";
        String url2="http://www.baidu.com";
        
        @BeforeMethod
        public void beforeTest(){        
            System.setProperty("webdriver.firefox.bin","D:\my program\firefox\firefox.exe");
            driver = new FirefoxDriver();
        }
     
        //输入框清除原有文字并输入内容,单击按钮
        @Test
        public void testInput(){
            driver.get(url1);
            WebElement input =driver.findElement(By.id("search_input"));
            input.clear();
            input.sendKeys("寒战");
            try{
                Thread.sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            WebElement btn =driver.findElement(By.id("search_btn"));
            btn.click();
            System.out.println(driver.getTitle());
        }
            
        //单选下拉列表
        @Test
        public void operateDropList(){
            Select dropList = new Select(driver.findElement(By.name("fruit")));
            Assert.assertFalse(dropList.isMultiple());
                
            //判断下拉框中选项的文字是否符合期望
            List except_options = Arrays.asList(new String[]{"桃子","香蕉","橘子","猕猴桃"});
            List actual_options=new ArrayList();
            for(WebElement option:dropList.getOptions()){
                actual_options.add(option.getText());
                Assert.assertEquals(except_options.toArray(),actual_options.toArray());
            }            
            String firstText = dropList.getFirstSelectedOption().getText();//获取第一个被选中的选项
            Assert.assertEquals("桃子", firstText);    
            dropList.selectByIndex(3);//通过索引选中第四个选项
            String forthText = dropList.getFirstSelectedOption().getText();
            Assert.assertEquals("猕猴桃", forthText);    
            dropList.selectByValue("xiangjiao");//通过value选择选项
            String xjText = dropList.getFirstSelectedOption().getText();
            Assert.assertEquals("香蕉", xjText);        
            dropList.selectByVisibleText("橘子");//通过选项的文字选择
            String jzText = dropList.getFirstSelectedOption().getText();
            Assert.assertEquals("橘子", jzText);        
        }
            
        //操作多选列表
        @Test
        public void operateMutipleDropList(){
            Select dropList = new Select(driver.findElement(By.name("fruit")));
            Assert.assertTrue(dropList.isMultiple());
                
            dropList.selectByIndex(3);
            dropList.selectByValue("xiangjiao");
            dropList.selectByVisibleText("橘子");
            dropList.deselectAll();
            dropList.selectByIndex(3);
            dropList.selectByValue("xiangjiao");
            dropList.selectByVisibleText("橘子");
            dropList.deselectByIndex(3);
            dropList.deselectByValue("xiangjiao");
            dropList.deselectByVisibleText("橘子");    
        }
            
        //操作单选框
        @Test
        public void operateRadio(){
            //直接操作单个
            WebElement radioOption = driver.findElement(By.id("test"));
            if(!radioOption.isSelected()){
                radioOption.click();
                Assert.assertTrue(radioOption.isSelected());
            }
            //遍历单选框的属性判断
            List fruits = driver.findElements(By.name("fruit"));    
            for(WebElement fruit:fruits){
                if(fruit.getAttribute("value").equals("pingguo")){
                    if(!fruit.isSelected()){
                        fruit.click();
                        Assert.assertTrue(fruit.isSelected());                
                    }
                    break;
                }
            }
        }
            
        //操作复选框
        @Test
        public void operateCheckBox(){
            WebElement orangeCb = driver.findElement(By.id(""));
            if(!orangeCb.isSelected()){
                orangeCb.click();
                Assert.assertTrue(orangeCb.isSelected());
            }
            if(orangeCb.isSelected()){
                orangeCb.click();
                Assert.assertTrue(orangeCb.isSelected());
            }
            List fruits = driver.findElements(By.name("fruit"));    
            for(WebElement fruit:fruits){
                fruit.click();
            }        
        }
        
        //检查页面元素的文本是否出现
        @Test
        public void isElementTextPresent(){
            WebElement text = driver.findElement(By.className("f1"));
            String contentText = text.getText();
            Assert.assertEquals("欢迎来到xxx网!",contentText);
            Assert.assertTrue(contentText.contains("xxx网"));
            Assert.assertTrue(contentText.startsWith("欢迎"));
            Assert.assertTrue(contentText.endsWith("xxx网!"));
        }*/
     
        //查看页面元素的属性
        @Test
        public void getWebElementAttribute(){
            driver.get(url1);
            WebElement input = driver.findElement(By.id(""));
            String inputString = "寒战";
            input.sendKeys(inputString);
            String inputText = input.getAttribute("value");
            Assert.assertEquals(inputString, inputText);
        }
        
        //隐式等待
        @Test
        public void testImplictWait(){
            driver.get(url1);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            
            try{
                WebElement searchInputBox = driver.findElement(By.id(""));
                WebElement searchButton= driver.findElement(By.id(""));
                searchInputBox.sendKeys("输入框被成功找到");
                searchButton.click();            
            }catch(NoSuchElementException e){
                Assert.fail("没有找到搜索的输入框");
                e.printStackTrace();
            }
        }
        
        //常用显示等待
        @Test
        public void testExplictWait(){
            WebDriverWait wait = new WebDriverWait(driver,10);
            wait.until(ExpectedConditions.titleContains("水果"));
            System.out.println("网页标题出现了水果的关键词");
            
            WebElement select = driver.findElement(By.id(""));
            wait.until(ExpectedConditions.elementToBeSelected(select));
            System.out.println("下拉列表的选项目前处于选中状态");
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='checkbox']")));
            System.out.println("页面复选框处于显示和可选状态");
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//p")));
            System.out.println("页面的P标签已显示");
            WebElement p = driver.findElement(By.xpath("//p"));
            wait.until(ExpectedConditions.textToBePresentInElement(p, "爱吃的水果"));
            System.out.println("页面P标签包括文本“爱吃的水果” ");       
        }
        
        //判断页面元素是否存在  
        public boolean isElementPresent(By by){
            try{
                driver.findElement(by);
                return true;
            }catch(NoSuchElementException e){
                return false;
            }
        }
        @Test
        public void testIsElementPresent(){
            driver.get(url2);
            if(isElementPresent(By.id("kw"))){
                WebElement searchInputBox = driver.findElement(By.id("kw"));
                if(searchInputBox.isEnabled()==true){
                    searchInputBox.sendKeys("搜索框被成功找到");
                }else{
                    Assert.fail("搜索框未找到");
                }
            }
        }
     
        @AfterMethod
        public void afterMethod() {
            driver.quit();
        }
    }
  • 相关阅读:
    jython resources
    Installing a Library of Jython ScriptsPart of the WebSphere Application Server v7.x Administration Series Series
    jython好资料
    ulipad install on 64bit win7 has issue
    an oracle article in high level to descibe how to archtichre operator JAVA relevet project
    table的宽度,单元格内换行问题
    Linux常用命令大全
    dedecms系统后台登陆提示用户名密码不存在
    登录织梦后台提示用户名不存在的解决方法介绍
    Shell常用命令整理
  • 原文地址:https://www.cnblogs.com/vanya/p/6742794.html
Copyright © 2011-2022 走看看