请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

使用@Parameters获取不到值

提示(Parameter ‘url’ is required by BeforeClass on method beforeClass but has not been marked @Optional or defined) 有拿老师的代码运行也这样,直接执行testng.xml或者使用@Optional都能正确执行,是否是testng版本的问题?用的7.0

package com.gcf.runcase;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.gcf.Immoc.TestNGListenerScreen;
import com.gcf.handle.LoginHandle;

@Listeners({TestNGListenerScreen.class})
public class LoginCase{
public static Logger logger = Logger.getLogger(LoginCase.class);

public WebDriver driver;
LoginHandle loginhandle;

  @Parameters({"url"})
  @BeforeClass
  public void beforeClass(String url) {
	  PropertyConfigurator.configure("log4j.properties");
	  System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
	  System.out.println("this is beforeClass");
	  driver = new ChromeDriver();
		driver.get(url);
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		loginhandle = new LoginHandle(driver);
		loginhandle.ClickSigninButton();
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  }

  @AfterClass
  public void afterClass() {
	  System.out.println("this is afterClass");
	  driver.close();
  }
  
/*@Test
  public void TestLoginerror() {
	  System.out.println("this is case to test fail");
	  loginhandle.SendEmail("guocaif@cn.ibm.com");
	  loginhandle.SendPassword("xiaofeng1231111");
	  //loginhandle.ClickSevenBox();
	  loginhandle.ClickLoginButton();
	  try { 
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  String username = loginhandle.GetUserText();
	  Assert.assertEquals(username, "慕少5264998");
	  
  }*/

//默认没有在XML中取到值, 使用@Optional("guocaif@cn.ibm.com,111111")	
  	@Parameters({"username","password"})
	@Test
	  public void TestLoginSuccess(String username,String password) {
		  System.out.println("this is case to test TestLoginSuccess");
		  loginhandle.SendEmail(username);
		  loginhandle.SendPassword(password);
		  //loginhandle.ClickSevenBox();
		  loginhandle.ClickLoginButton();
		  try { 
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  String username1 = loginhandle.GetUserText();
		  Assert.assertEquals(username1, "慕少5264998");
		  
	  }
	
  @BeforeTest
  public void beforeTest() {
	  System.out.println("this is beforeTest");
	 
  }

  @AfterTest
  public void afterTest() {
	  System.out.println("this is afterTest");
	  
  }

}

<?xml version="1.0" encoding="UTF-8"?>

正在回答 回答被采纳积分+3

1回答

Mushishi 2020-05-16 11:39:06

你把你xml发一下,这个代码看着时没问题得。报错的也是说参数没定义。然后你//默认没有在XML中取到值, 使用@Optional("guocaif@cn.ibm.com,111111")


这个也增加到url那里看是否还报错。

0 回复 有任何疑惑可以回复我~
  • 提问者 GUOCAIFENG #1
    使用Optional可以执行,不会报错, 以下是XML的代码:
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Suite">
    	<parameter name="username" value="guocaif@cn.ibm.com"></parameter>
    	<parameter name="password" value="xiaofeng123333"></parameter>
    	<parameter name="url" value="http://www.imooc.com"></parameter>
      <test name="Test">
        <classes>
          <class name="com.gcf.runcase.LoginCase">
          	<methods>
          		<include name="TestLoginSuccess"></include>
          	</methods>
          </class>
        </classes> 
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    回复 有任何疑惑可以回复我~ 2020-05-18 17:53:28
  • Mushishi 回复 提问者 GUOCAIFENG #2
    看着没问题,你把option加上,然后他俩的地址不一样,你看一下你定义之后取得是哪个,如果是定义得说明xml文件没取到。但是你下面username你要看能取到么,如果取不到说明这里变化了。
    回复 有任何疑惑可以回复我~ 2020-05-18 21:58:00
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信