Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何运行将在执行main方法后继续工作的测试类_Java_Selenium - Fatal编程技术网

Java 如何运行将在执行main方法后继续工作的测试类

Java 如何运行将在执行main方法后继续工作的测试类,java,selenium,Java,Selenium,我有一个测试类,缅因州在其中执行代码,我在其中获取测试中必须使用的测试数据,如何首先运行缅因州方法,然后运行类中的其余代码。如果我通过testng.xml运行这个类,那么main方法根本就不会启动。如果我运行main,其余的代码也不会启动 package testClassPackage; public class testWeatherChrome { public static String parcedData; public static void main(Stri

我有一个测试类,缅因州在其中执行代码,我在其中获取测试中必须使用的测试数据,如何首先运行缅因州方法,然后运行类中的其余代码。如果我通过testng.xml运行这个类,那么main方法根本就不会启动。如果我运行main,其余的代码也不会启动

package testClassPackage;

public class testWeatherChrome {
    public static String parcedData;

    public static void main(String[] args) throws IOException {

        String sURL = "http://api.openweathermap.org/data/2.5/forecast/?q=Odessa,ua&APPID=518a64dd48106aa542464d3bd94d12ce"; //just a string


        URL url = new URL(sURL);
        URLConnection request = url.openConnection();
        request.connect();


        JsonParser jp = new JsonParser();
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
        JsonObject rootobj = root.getAsJsonObject();
        JsonArray message = rootobj.get("list").getAsJsonArray();
        Map<String, String> data = new TreeMap<>();

        for (JsonElement lst : message) {
            JsonObject lstObject = lst.getAsJsonObject();
            JsonObject el = (JsonObject) lstObject.get("main");
//            System.out.println(lstObject.get("dt_txt").getAsString() + " " + el.get("temp").getAsString());
            if (lstObject.get("dt_txt").getAsString().contains(" 12")) {

                data.put("Дата " + lstObject.get("dt_txt").getAsString(), "Прогноз День:" + el.get("temp").getAsString());
            } else if (lstObject.get("dt_txt").getAsString().contains(" 21")) {
                data.put("Дата " + lstObject.get("dt_txt").getAsString(), "Прогноз Ночь:" + el.get("temp").getAsString());
            }

        }
        Gson gson = new Gson();
        Type gsonType = new TypeToken<TreeMap>() {
        }.getType();
        String gsonData = gson.toJson(data, gsonType);
        parcedData = gsonData;
        System.out.println("main");

    }
        @BeforeClass
        public void setUp () {
            initDriver.getInstance("chrome");
        }

    @Test
    public void getTitle() throws InterruptedException {
        String URL ="https://accounts.google.com/signin/v2/identifier?hl=ru&passive=true&continue=https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl&flowName=GlifWebSignIn&flowEntry=ServiceLogin";
        initDriver.driver.get(URL);
        initDriver.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        initDriver.driver.findElement(By.xpath("//input[@id=\"identifierId\"]")).sendKeys("lesha.test111@gmail.com");
        initDriver.driver.findElement(By.xpath("//span[text()=\"Далее\"]")).click();
        WebDriverWait wait = new WebDriverWait(initDriver.driver, 10);
        WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type=\"password\"]")));
//        initDriver.driver.findElement(By.xpath("//input[@type=\"password\"]")).sendKeys("Leshasugurov222");
        passwordElement.sendKeys("Leshasugurov222");
        initDriver.driver.findElement(By.xpath("//span[text()=\"Далее\"]")).click();
        initDriver.driver.findElement(By.xpath("//a[text()=\"Почта\"]")).click();
        initDriver.driver.findElement(By.xpath("//div[text()=\"Написать\"]")).click();
        initDriver.driver.findElement(By.xpath("//textarea[@aria-label=\"Кому\"]")).sendKeys("leshaa.test333@gmail.com");
        initDriver.driver.findElement(By.xpath("//input[@aria-label=\"Тема\"]")).sendKeys("Погода на неделю для Одессы");
        System.out.println(WeatherParse.parcedData);
//        initDriver.driver.findElement(By.xpath("//div[@aria-label=\"Тело письма\"]")).sendKeys(WeatherParse.parcedData);


    }


//    @AfterClass
//    public void close() {
//        initDriver.quit();
//    }

}
包testClassPackage;
公共类testWeatherChrome{
公共静态字符串parcedData;
公共静态void main(字符串[]args)引发IOException{
字符串sURL=”http://api.openweathermap.org/data/2.5/forecast/?q=Odessa,ua&APPID=518a64dd48106aa542464d3bd94d12ce;//只是一个字符串
URL=新URL(sURL);
URLConnection request=url.openConnection();
request.connect();
JsonParser jp=新的JsonParser();
JsonElement root=jp.parse(新的InputStreamReader((InputStream)request.getContent());
JsonObject rootobj=root.getAsJsonObject();
JsonArray message=rootobj.get(“list”).getAsJsonArray();
映射数据=新树映射();
for(JsonElement lst:message){
JsonObject lstObject=lst.getAsJsonObject();
JsonObject el=(JsonObject)lstObject.get(“main”);
//System.out.println(lstObject.get(“dt_txt”).getAsString()+“”+el.get(“temp”).getAsString());
if(lstObject.get(“dt_txt”).getAsString()包含(“12”)){
data.put(“Пааа”+lstObject.get(“dtаtxt”).getAsString(),“ППзааззааа:”el.get(“temp”).getAsString());
}else if(lstObject.get(“dt_txt”).getAsString()包含(“21”)){
data.put(“Паааа”+lstObject.get(“dtаtxt”).getAsString(),“Пзззч”:“+el.get(“temp”).getAsString());
}
}
Gson Gson=新的Gson();
类型gsonType=newtypetoken(){
}.getType();
字符串gsonData=gson.toJson(数据,gsonType);
parcedData=gsonData;
System.out.println(“主”);
}
@课前
公共无效设置(){
initDriver.getInstance(“chrome”);
}
@试验
public void getTitle()引发InterruptedException{
字符串URL=”https://accounts.google.com/signin/v2/identifier?hl=ru&passive=true&continue=https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl&flowName=GlifWebSignIn&flowEntry=ServiceLogin";
initDriver.driver.get(URL);
initDriver.driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
initDriver.driver.findElement(By.xpath(//input[@id=\“identifierId\”])。test111@gmail.com");
initDriver.driver.findElement(By.xpath(“//span[text()=\”Баааа\“])。单击();
WebDriverWait wait=新的WebDriverWait(initDriver.driver,10);
WebElement passwordElement=wait.until(ExpectedConditions.elementtobelickable(By.xpath(//input[@type=\“password\”]);
//initDriver.driver.findelelement(By.xpath(//input[@type=\“password\”)).sendKeys(“Leshasugurov222”);
sendKeys(“Leshasugurov222”);
initDriver.driver.findElement(By.xpath(“//span[text()=\”Баааа\“])。单击();
initDriver.driver.findElement(By.xpath(“//a[text()=\”Пчаа\“])。单击();
initDriver.driver.findElement(By.xpath(“//div[text()=\”CharStyleаПCharStyleаCharStyle\\”))。单击();
initDriver.driver.findElement(By.xpath(“//textarea[@aria label=\“κБМц\”]”)。test333@gmail.com");
initDriver.driver.findElement(By.xpath(//input[@aria label=\'tаМаа\'])).sendKeys(“Пааааааааааа1072;
System.out.println(WeatherParse.parcedata);
//initDriver.driver.findElement(By.xpath(“//div[@aria label=\“CharStyle\\\\\\\\”)).sendKeys(WeatherParse.parcedata);
}
//@下课
//公众假期结束(){
//initDriver.quit();
//    }
}

如果运行testng.xml,很明显testng.xml文件将不尊重main方法(因为它本来就不应该这样)

解决方案:

1) 使用
@BeforeSuite
注释并执行优先任务


2) 您还可以使用testng.xml文件并相应地定义测试序列。

阅读一篇关于JUnit的不错的教程对您很有帮助。测试类中不应该有main方法,例如,将main方法代码移动到@BeforeClass,以便它在测试之前执行。在setup方法中调用main方法。