使用 Selenium API(2)

来源:互联网 发布:网络mg老虎机出分原理 编辑:程序博客网 时间:2024/06/08 10:17
9. 使用 RemoteWebDriver/Grid 进行截图

当测试运行在 RemoteWebDriver/Grid 时就不能使用 TakeScreenshot 接口来进行截图。
但是我们可以使用 TakesScreenshot 接口的 Augmenter类来截取 RemoteWebDriver
中的屏幕


如何实现:

创建一个 RemoteWebDriver 的测试。
加入下例代码到测试中去:
driver = new Augmenter().augment(driver);
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
Augmenter 类通过增加了多种接口,包括 TakesScreenshot 接口增加了RemoteWebDriver。

package com.example.tests;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
public class test {

@Test
public void testRemoteWebDriverScreenShot() {
          //指定使用的浏览器
          DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
          WebDriver driver = null;
          try {
                driver = new RemoteWebDriver( //我使localhost本地来测试
                new URL("http://localhost:4444/wd/hub"), capability);
                } catch (MalformedURLException e) {
                      e.printStackTrace();
         }
        driver.get("http://www.sina.com.cn");
        //对远程系统进行截图
        driver = new Augmenter().augment(driver);
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
              FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
             } catch (IOException e) {
                  e.printStackTrace();
        }
}
}


10. 将浏览器窗口最大化

在 Selenium RC 中我们可以使用 windowMaximize()方法来最大化浏觅器。从 2.21 版本
后,Selenium WebDriver 也可以将浏览器最大化了。


如何实现:

通过调用 maximize()方法可以将浏觅器窗口最大化。
package com.example.tests;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Selenium2 {
          @Test
          public void testTakesScreenshot() {
                    WebDriver driver = new InternetExplorerDriver();
                    driver.get("http://www.baidu.com");
                    driver.manage().window().maximize();
                    driver.close();
          }
}


11. 自动选择下拉列表

Selenium WebDriver 对于下拉列表可以使用指定的Select 类替代 WebElement 类。
Select 类提供了多种方法和属性来和 HTML 中的<select>元素进行交互。
在这个秘籍中,我们将要使用 Select 类来操作下拉列表。


如何实现:

package com.example.tests;
import static org.junit.Assert.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium2 {
          @Test
          public void testDropdown() {
                    WebDriver driver = new FirefoxDriver();
                    driver.get("D:\\demo\\Droplist.html");
                    //得到下拉列表框
                    Select make = new Select(driver.findElement(By.name("make")));
                    //验证下拉列表的不支持多选
                    assertFalse(make.isMultiple());
                    //验证下拉列表的数量
                    assertEquals(4,make.getOptions().size());
                    //命名用可见的本文来选择选项
                    make.selectByVisibleText("Honda");
                    //通过value属性来选择选项
                    make.selectByValue("Audi");
                    //通过索引来选择选项
                    make.selectByIndex(2);
                    driver.close();
         }
}

创建另一个多选下拉框的测试例子,执行一些基本的检查然后调用方法进行下拉列表的多选
操作,验证所选的选项然后调用取消选项的方法,取消已选的选项。


12. 检查下拉列表中的选项

在之前的秘籍中,我们知道了如何选择下拉列表中的选项,并做了一些验证的工作,我们还
需要验证被选中的选项是不是正确的,无论是默认的还是用户选择的。


如何实现:

在之前的例子中,我们需要加一些步骤,所以对上面的例子做一些修改(加粗部分为新增)
修改 testDropdown()

package com.example.tests;
import static org.junit.Assert.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium2 {
          @Test
          public void testDropdown() {
                   WebDriver driver = new FirefoxDriver();
                   driver.get("D:\\demo\\Droplist.html");
                   //得到下拉列表框
                   Select make = new Select(driver.findElement(By.name("make")));
                   //验证下拉列表的不支持多选
                   assertFalse(make.isMultiple());
                   //验证下拉列表的数量 
                   assertEquals(4,make.getOptions().size());
                   //使用可见的本文来选择选项
                   make.selectByVisibleText("Honda");

                   assertEquals("Honda",make.getFirstSelectedOption().getText());
                   //通过value属性来选择选项
                   make.selectByValue("Audi");
                   assertEquals("Audi",make.getFirstSelectedOption().getText());
                   //通过索引来选择选项
                   make.selectByIndex(2);
                   assertEquals("BMW",make.getFirstSelectedOption().getText());
          }
}


同样的修改 testMultipleSelectLis()
package com.example.tests;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium2 {
          @Test
          public void testMultipleSelectLis() {
                   WebDriver driver = new FirefoxDriver();
                   driver.get("D:\\demo\\Droplist.html");
                   //得到下拉列表框
                   Select color = new Select(driver.findElement(By.name("color")));
                   //验证下拉列表支持多选
                   assertTrue(color.isMultiple());
                   //验证下拉列表的数量
                   assertEquals(4,color.getOptions().size());
                   //用可见的本文来选择选项
                   color.selectByVisibleText("Black");
                   color.selectByVisibleText("Red");
                   color.selectByVisibleText("Silver");
                   //验证所选的选项
                   List<String> exp_sel_options =
                                     Arrays.asList(new String[] {"Black","Red","Silver"});
                   List<String> act_sel_options = new ArrayList<String>();
                   for(WebElement option:color.getAllSelectedOptions()){
                             act_sel_options.add(option.getText());
                   }

                   //验证选择的选项和我们期望的是一样的
                   assertArrayEquals
                   (exp_sel_options.toArray(), act_sel_options.toArray());

                   //验证3个选项已经被选择了
                   assertEquals(3, color.getAllSelectedOptions().size());
                   //通过可见的文本取消已选选项
                   color.deselectByVisibleText("Silver");
                   assertEquals(2, color.getAllSelectedOptions().size());
                   //通过value属性取消已选选项
                   color.deselectByValue("red");
                   assertEquals(1, color.getAllSelectedOptions().size());
                   //通过选项索引取消已选选项
                   color.deselectByIndex(0);
                   assertEquals(0, color.getAllSelectedOptions().size());
                   }
}


如果只是单选的下拉列表,通过getFirstSelectedOption()就可以得到所选择的选项,
再调用 getText()就可以得到本文。如果是多选的下拉列表,使用
getAllSelectedOptions()得到所有已选择的选项,此方法会返回元素的集合。使用
assertArrayEquals()方法来对比期望和实际所选的选项是否正确。调用
getAllSelectedOptions().size()方法来判断已选的下拉列表选项数量。如果想检查
某一个选项是否被选择了,可以使用 assertTrue(act_sel_options.contains("Red")) 方
法。


13. 自动选择单选按钮

Selenium WebDriver 的 WebElement 类支持单选按钮和按钮组。我们可以通过 click()
方法来选择单选按钮和取消选择,使用 isSelect()方法来判断是否选中了单选按钮。


如何实现:

package com.example.tests;
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium2 {
          @Test
          public void testRadioButton() {
                  WebDriver driver = new FirefoxDriver();
                  driver.get("D:\\demo\\RadioButton.html");
                  //使用value值来定位单选按钮
                  WebElement apple = driver.findElement(By.cssSelector("input[value='Apple']"));
                  //检查是否已选择,如果没有则点击选择
                  if(!apple.isSelected()){
                               apple.click();
                  }
                  //验证apple选项已经选中
                  assertTrue(apple.isSelected());

                  //也可以得到所有的单选按钮
                  List<WebElement> fruit = driver.findElements(By.name("fruit"));
                  //查询Orange选项是否存在,如果存在则选择
                  for(WebElement allFruit : fruit){
                        if(allFruit.getAttribute("value").equals("Orange")){
                             if(!allFruit.isSelected()){
                                 allFruit.click();
                               assertTrue(allFruit.isSelected());
                               break;
                              }
                         }
                   }
         }
}


14. 自动选择多选项框

Selenium WebDriver的 WebElement 类也支持多选框。我们可以使用 click()方法来选
择和取消选择,使用 isSelect()方法来判断是否选中。


如何实现:

package com.example.tests;
import static org.junit.Assert.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium2 {
          @Test
          public void testRadioButton() {
                  WebDriver driver = new FirefoxDriver();
                  driver.get("D:\\demo\\checkbox.html");
                  //使用value值来选择单选按钮
                  WebElement apple = driver.findElement(By.cssSelector("input[value='Apple']"));
                  WebElement pear = driver.findElement(By.cssSelector("input[value='Pear']"));
                  WebElement orange = driver.findElement(By.cssSelector("input[value='Orange']"));

                  //检查是否已选择,如果没有则点击选择
                  if(!apple.isSelected()){
                        apple.click();
                  }
                  if(!pear.isSelected()){
                        pear.click();
                  }
                  if(!orange.isSelected()){
                       orange.click();
                  }
                  //验证选项已经选中
                  assertTrue(apple.isSelected());
                  assertTrue(pear.isSelected());
                  assertTrue(orange.isSelected());
                  //再次点击apple多选框,取消选择
                  if(apple.isSelected()){
                      apple.click();
                  }

                  assertFalse(apple.isSelected());
          }
}


15. 处理 windows 的过程

Selenium WebDriver java 提供了 windowsUtils 类来和 Windows 操作系统交互。在测
试开始时,我们需要关掉已经一些进程。


如何实现:

@Before
public void setUp()
{
WindowsUtils.tryToKillByName("firefox.exe");
driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
}
我们可以使用 tryToKillByName 方法来关闭任何的 windows 的进程。如果这个进程不存
在则会抛出一个异常,但是,测试还是会正常的执行下去。



原创粉丝点击