gpt4 book ai didi

java - 如何选择浏览器,然后从用户那里选择我的特定 URL

转载 作者:行者123 更新时间:2023-11-30 07:06:51 25 4
gpt4 key购买 nike

我是使用 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");



}
}

最佳答案

我修复了一些问题并重新组织了代码。这是使用 Selenium 的非典型方式。大多数脚本不会询问用户想要使用什么浏览器等。如果他们拼错浏览器名称,您也会遇到问题。我希望(?)您这样做只是为了尝试 Selenium 的乐趣。

我修复的一些问题:

  1. 您可以重复使用阅读器
  2. 删除了一些额外的变量等。
  3. 将驱动程序的用户输入转换为函数 getDriver(),并返回一个可以重用的 WebDriver 实例。
  4. 将 IE 添加到enumgetDriver()
  5. 修正了一些错别字和拼写错误。

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
}
}

关于java - 如何选择浏览器,然后从用户那里选择我的特定 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com