首页 > 代码库 > Selenium_java-2 Some Demo for Firefox &Chrome

Selenium_java-2 Some Demo for Firefox &Chrome

cool thing happen today :) envrioment set up and seleniumn can run. let‘s explore the selenium-webdirver

save the comand as bat, so next time just double click on it.than it will up the senelium server

selenium-stand along.jar is a webdrive (about webdrive and romete control, please see wike or baidu or google

  1. Fire fox demo
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.chrome.*;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;public class Web {    public static void main(String[] args) {        //创建一个 firefox driver 实例WebDriver driver = new FirefoxDriver();        //打开测试网址driver.get("https://apps.na.collabserv.com/");//定义用户名和密码文本框        WebElement username=driver.findElement(By.id("username"));        WebElement password=driver.findElement(By.id("password"));        //输入用户名和密码username.sendKeys("autouser01@e3yunmail.mail.lotuslive.com");        // WebElement continuevisit=driver.findElement(By.id("continue"));        password.sendKeys("test");        //点击 login 登录        WebElement login=driver.findElement(By.id("submit_form"));        login.click();        //设置页面等待直到出现 Mail 链接(new WebDriverWait(driver, 500)).until(new ExpectedCondition<WebElement>(){public WebElement apply(WebDriver dr) {                return dr.findElement(By.linkText("Mail"));            }        });        //登出        WebElement logout=driver.findElement(By.linkText("Log Out"));        logout.click();        //关闭浏览器        driver.quit();    }}

2, Chrome Demo- Note chrome is different with IE and FIrefox. It is need to download a chromedriver.exe under same root as chrome.exe also need to set property. compare the two demo, find difference

import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.chrome.*;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;public class Web {    public static void main(String[] args) {        //创建一个 firefox driver 实例        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" );WebDriver driver = new ChromeDriver();        //打开测试网址driver.get("https://apps.na.collabserv.com/");//定义用户名和密码文本框        WebElement username=driver.findElement(By.id("username"));        WebElement password=driver.findElement(By.id("password"));        //输入用户名和密码username.sendKeys("autouser01@e3yunmail.mail.lotuslive.com");        // WebElement continuevisit=driver.findElement(By.id("continue"));        password.sendKeys("test");        //点击 login 登录        WebElement login=driver.findElement(By.id("submit_form"));        login.click();        //设置页面等待直到出现 Mail 链接(new WebDriverWait(driver, 500)).until(new ExpectedCondition<WebElement>(){public WebElement apply(WebDriver dr) {                return dr.findElement(By.linkText("Mail"));            }        });        //登出        WebElement logout=driver.findElement(By.linkText("Log Out"));        logout.click();        //关闭浏览器        driver.quit();    }}

 

helpful address:

 http://www.ibm.com/developerworks/cn/web/1309_fengyq_seleniumvswebdriver/

http://www.open-open.com/lib/view/open1354764154148.html

http://qa.blog.163.com/blog/static/19014700220122231779/

 

Selenium_java-2 Some Demo for Firefox &Chrome