我正在尝试使用我在一个类中使用的变量,然后在另一个类中使用该变量。 所以这是我的代码,它基本上从网址中获取一个ID
public class GameId {
public String game_id() {
String currentURL = Drivers.getDriver().getCurrentUrl();
String[] arrayURL = currentURL.split("/");
int arrLength = arrayURL.length;
final String gameID;
gameID = arrayURL[arrLength - 1];
System.out.println(gameID);
return gameID;
}
}
我试图在其他类的测试中使用它,但它给我的答案不尽相同,我试图断言该游戏ID包含在其他页面的url中,但是该函数再次运行,因此价值发生变化。
public void guest_login_from_pick_screen(){
GameId gameid = new GameId();
WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
gameid.game_id();
wait.until(ExpectedConditions.urlMatches("https://web-game-stage.sportdec.com/games/"+ gameid.game_id()+"/join/"));
}
我在测试中同时使用了这两个函数,但我希望在第二个实例(fixscreen.guest_login_from_pick_screen();)中使用第一个实例的值(gameid.game_id();)。这是完整的代码
@Test
public void join_game_already_logged_in () throws Exception {
Header header = new Header();
Thread.sleep(3000);
GameLobby gamelobby = new GameLobby();
gamelobby.clickElementWithName("Test Game");
gamelobby.select_game();
LeaderBoard leaderboard = new LeaderBoard();
GameId gameid = new GameId();
gameid.game_id();
leaderboard.numberOfUsers();
leaderboard.joinGame();
FixturesScreen fixscreen = new FixturesScreen();
fixscreen.guest_login_from_pick_screen();
fixscreen.four_picks_make();
Thread.sleep(4000);
fixscreen.picks_match_total();
fixscreen.submit_picks();
Login login = new Login();
login.select_register_from_login();
Register register = new Register();
register.register_in_pick_screen();
fixscreen.submit_picks();
Thread.sleep(3000);
PickReceipt pickReceipt = new PickReceipt();
pickReceipt.your_in_the_game();
}
答案 0 :(得分:1)
您需要将GameID类更改为单例类,并需要使用封装概念。因此,它将创建一个实例,您可以按以下方式获取当前游戏ID
您必须如下更改您的GameID
public class GameId {
private String currentGameId;
private static GameId gameID;
public static GameId getInstance(){
if(gameID==null){
gameID=new GameId();
}
return gameID;
}
public void setCurrentGameId(String id){
currentGameId=id;
}
public String getCurrentGameId(){
return currentGameId;
}
public void game_id() {
String currentURL = Drivers.getDriver().getCurrentUrl();
String[] arrayURL = currentURL.split("/");
int arrLength = arrayURL.length;
final String gameID;
gameID = arrayURL[arrLength - 1];
System.out.println(gameID);
this.currentGameId=gameID;
}
}
您只需调用getCurrentGameId
方法即可获取当前的CaseID。
public void guest_login_from_pick_screen(){
GameId gameid =GameId.getInstance();
WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
//gameid.currentGameId();//It will give the current CaseID from the GameID class
wait.until(ExpectedConditions.urlMatches("https://web-game-stage.sportdec.com/games/"+gameid.getCurrentGameId()+"/join/"));
}
在您的测试方法中,应对其进行如下修改
LeaderBoard leaderboard = new LeaderBoard();
GameId gameid = GameId.getInstance(); //It will create a new instance
gameid.game_id();//This method will be executed once and gameID will be updated in a variable called currentGameId.So, we can simply call the getCurrentGameId() method to get the current ID.
答案 1 :(得分:0)
请使用以下代码:-
public void guest_login_from_pick_screen(){
GameId gameid = new GameId();
WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
String var = gameid.game_id();
wait.until(ExpectedConditions.urlMatches("https://web-game-stage.sportdec.com/games/"+ var +"/join/"));
}
答案 2 :(得分:0)
请按如下所示设置变量static
:
public static String url="www.example.com";
然后可以在任何需要的地方使用它。
答案 3 :(得分:0)
public class GameId {
public static String gameId = "";
public String game_id() {
String currentURL = Drivers.getDriver().getCurrentUrl();
String[] arrayURL = currentURL.split("/");
int arrLength = arrayURL.length;
final String gameID;
gameID = arrayURL[arrLength - 1];
System.out.println(gameID);
gameId = gameID;
return gameID;
}
}
一次创建GameId对象,然后从下一次开始使用,就像这样GameId.gameId