我是使用Selenium进行自动化测试的新手。这就是为什么,我面临着从用户输入中获取浏览器和URL的问题。比如,当我的代码被执行时,第一个用户输入被选为您的组织,第二个被选为您的URL,如Google,Facebook等。
public class Pages {
private enum Organization {
Firefox, Chrom
}
static Organization obj;
private static Pages Chrom;
private static Scanner reader;
private static Scanner reader_1;
public static void main(String[]args) throws InterruptedException {
char ch;
String org;
do
{
System.out.println("");
reader = new Scanner(System.in);
System.out.println("Please Enter The Browser Name :");
org = reader.nextLine();
obj=Organization.valueOf(org);
switch(obj)
{
case Firefox:
Pages.Firefoxdriver();
break;
case Chrom:
Pages.Chromedriver();
break;
default:
System.out.println("You Have Selected Wrong Browser ");
}
reader_1 = new Scanner(System.in);
System.out.println("Please a key followed by ENTER: \n ");
ch = reader_1.next().charAt(0);
} while (ch != 'q');
}
public static void Firefoxdriver(){
WebDriver driver = new FirefoxDriver();
driver.get("http://qa-0.ls.vu");
}
public static void Chromedriver(){
System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("http://qa-0.ls.vu");
}
public void IEdriver(){
File file =new File("C:/Users/mkv/workspace/ServerDrivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driverIE=new InternetExplorerDriver();
driverIE.navigate().to("http://www.msn.com");
}
}
答案 0 :(得分:1)
我修复了一些问题并重新组织了代码。这是使用Selenium的一种非典型方式。大多数脚本都不会询问用户他们想要使用哪种浏览器等。如果他们拼错浏览器名称,您也会遇到问题。我希望(?)你这样做是为了试试Selenium的乐趣。
我修复了一些事情:
reader
。getDriver()
,并返回可以重复使用的WebDriver
实例。enum
和getDriver()
。
public class Pages
{
private static WebDriver driver;
private static Scanner reader;
public static void main(String[] args) throws InterruptedException
{
char ch;
do
{
System.out.println();
reader = new Scanner(System.in);
System.out.print("Please enter the browser name: ");
driver = getDriver(reader.nextLine());
driver.get("http://qa-0.ls.vu");
System.out.print("Press q to quit followed by ENTER: ");
reader = new Scanner(System.in);
ch = reader.next().charAt(0);
} while (ch != 'q');
}
public static WebDriver getDriver(String browserName)
{
WebDriver driver = null;
Browsers browser = Browsers.valueOf(browserName.toUpperCase());
switch (browser)
{
case FIREFOX:
System.out.println("Launching Firefox");
driver = new FirefoxDriver();
break;
case CHROME:
System.out.println("Launching Chrome");
System.setProperty("webdriver.chrome.driver", "E:\\workspace\\chromeDriver\\chromeDriver.exe");
driver = new ChromeDriver();
break;
case IE:
System.out.println("Launching IE");
System.setProperty("webdriver.ie.driver", "C:/Users/mkv/workspace/ServerDrivers/IEDriverServer.exe");
driver = new InternetExplorerDriver();
break;
default:
System.out.println("You have selected a browser that isn't available.");
}
return driver;
}
private enum Browsers
{
FIREFOX, CHROME, IE
}
}