FearghalQA
FearghalQA

Reputation: 265

Use string outside test in selenium

Hi I'm trying to reuse a String that I'm returning from an api test, then reuse it in a different test. So this is the function being called:

public void return_fix()throws Exception {
    GetFixtures getfixtures = new GetFixtures();
    getfixtures.get_fixtures_between_dates();
}

and this is how the function works

public void get_fixtures_between_dates() throws Exception {
    String DATE_FORMAT;
    DATE_FORMAT = "YYYY-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance(); // today
    String todays_date = sdf.format(c1.getTime());

    String DATE_FORMAT1 = "YYYY-MM-dd";
    SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT1);
    Calendar c2 = Calendar.getInstance(); // today
    c2.add(Calendar.DATE, 1);
    String tomorrows_date = sdf1.format(c2.getTime());

    Map<String, String> additional_parameters = new HashMap<>();

    responseBody = call.getFixtureId(todays_date, tomorrows_date).execute();
    String response_body_string = responseBody.body().string();
    String[] tmp = response_body_string.split(",");
    String val1 = tmp[0];
    tmp = tmp[1].split(",");
    //Returns second game ID
    String val2 = tmp[0];
    System.out.println(val2);
}

I'm trying to use String val2 in a different method in the same test so after I run get_fixtures_between_dates the next function will be able to use the string, is there anyway I can use the string value?

Upvotes: 0

Views: 43

Answers (1)

Rushby
Rushby

Reputation: 889

Return the value in your current method:

public String get_fixtures_between_dates() throws Exception {
    String DATE_FORMAT;
    DATE_FORMAT = "YYYY-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance(); // today
    String todays_date = sdf.format(c1.getTime());

    String DATE_FORMAT1 = "YYYY-MM-dd";
    SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT1);
    Calendar c2 = Calendar.getInstance(); // today
    c2.add(Calendar.DATE, 1);
    String tomorrows_date = sdf1.format(c2.getTime());

    Map<String, String> additional_parameters = new HashMap<>();

    responseBody = call.getFixtureId(todays_date, tomorrows_date).execute();
    String response_body_string = responseBody.body().string();
    String[] tmp = response_body_string.split(",");
    String val1 = tmp[0];
    tmp = tmp[1].split(",");
    //Returns second game ID
    String val2 = tmp[0];
    System.out.println(val2);

return val2
}

Call and assign this within your other method:

    public String return_fix()throws Exception {

    GetFixtures getfixtures = new GetFixtures();
    String val2 = getfixtures.get_fixtures_between_dates();
}

Upvotes: 1

Related Questions