Reputation: 140
Im trying to use a variable the i have taken in one class and then use that variable in a different classes. So this is my code and it basically takes an id from a url
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;
}
}
Im trying to use this in my tests in a different class but it doesn't give me the same answer, im trying to assert that this game id is included in the url on a different page but the function is running again so the value changes.
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/"));
}
I use both of these functions in my test but i want the value of the first instance (gameid.game_id();) to be used in the second instance (fixscreen.guest_login_from_pick_screen();). Here is the full code
@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();
}
Upvotes: 1
Views: 79
Reputation: 786
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;
}
}
Create GameId object at once and then from next time use like this GameId.gameId
Upvotes: 0
Reputation: 2334
You need to change your GameID class as singleton class and need to use encapsulation concept.So, that it will create a single instance and you can get the currentgame Id as below
You have to change your GameID as below
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;
}
}
You can get the current CaseID by simply calling getCurrentGameId
method.
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/"));
}
In your test method, it should be modified as below
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.
Upvotes: 1
Reputation: 573
Please make variable static
as follows:
public static String url="www.example.com";
and then it can be used wherever you want.
Upvotes: 0
Reputation: 2180
please use the below code :-
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/"));
}
Upvotes: 0