Home > Back-end >  Splitting a cookie string and saving the split cookie value in a external csv file in Apache JMeter
Splitting a cookie string and saving the split cookie value in a external csv file in Apache JMeter

Time:02-04

Objective : I wanna "split" the extracted cookie from ";" and save the cookie value in a iterative manner for each user in a csv file, column wise, for my Apache JMeter code in Selenium Webdriver Sampler in java language. I am using a CSV Dataset config to fetch the username & password and would like to save the cookie value in a separate column.

I am unable to spilt the cookie and save using the following code snippet.

My code Snippet

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.junit.Assert;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import java.io.*;
import java.util.Arrays;
import java.io.FileWriter;
import java.util.List;
import java.io.Writer;
import java.lang.String;

// Login 
String title = driver.getTitle();
System.out.println(title);
Assert.assertEquals("Sign in to your account", title);
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='email']")));
driver.findElement(By.xpath(".//input[@type='email']")).click();
driver.findElement(By.xpath(".//input[@type='email']")).sendKeys(new String[] {WDS.vars.get("username")});
driver.findElement(By.xpath(".//input[@type='submit']")).click();
WebElement username = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//input[@type='password']")));
driver.findElement(By.xpath(".//input[@type='password']")).click();
driver.findElement(By.xpath(".//input[@type='password']")).sendKeys(new String[] {WDS.vars.get("password")});
driver.findElement(By.xpath(".//input[@type='submit']")).click();
Thread.sleep(3000L);
driver.findElement(By.xpath(".//input[@type='button']")).click();
Thread.sleep(3000L);

// Generating the cookie
String rawValue = driver.manage().getCookieNamed(".sampleCookie");
System.out.println(rawValue);
WDS.log.info(WDS.vars.get("rawValue"));
System.out.println("Login Successful");
WDS.log.info("Login Successful");

I am able to use the above code snippet to generate the cookie, but unable to print in JMeter Log console using WDS.log.info(WDS.vars.get("rawValue"));

Info: 2023-02-03 14:34:08,656 INFO c.g.j.p.w.s.WebDriverSampler: null

I am unable to split the cookie via below mentioned code.

//Splitting the cookie 

//Split cookie string from semicolon
String[] cookie = rawValue.split(";");

Error: 2023-02-03 15:38:44,557 ERROR c.g.j.p.w.s.WebDriverSampler: Sourced file: inline evaluation of: import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Typed variable declaration : Error in method invocation: Method split( java.lang.String ) not found in class'org.openqa.selenium.Cookie' : at Line: 53 : in file: inline evaluation of: import org.openqa.selenium.; import org.openqa.selenium.support.ui.; import or . . . '' : rawValue.split ( ";" ) in inline evaluation of: ``import org.openqa.selenium.; import org.openqa.selenium.support.ui.;

I am unable to save the split cookie value or the raw cookie Value in that case via below mentioned code.

//Write the cookie value to a csv file

FileWriter fstream = new FileWriter("C:\\Users\\Scripts\\TestData\\Cookie.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(WDS.vars.get("rawValue"));
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

Error: 2023-02-03 15:45:13,652 ERROR c.g.j.p.w.s.WebDriverSampler: Sourced file: inline evaluation of: import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; import or . . . '' : Method Invocation out.write : at Line: 54 : in file: inline evaluation of: import org.openqa.selenium.; import org.openqa.selenium.support.ui.; import or . . . '' : out .write ( WDS .vars .get ( "rawValue" ) )

Target exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null in inline evaluation of: ``import org.openqa.selenium.; import org.openqa.selenium.support.ui.;

I have imported the required classes and have tried executing this code snippets individually with the login code, did'nt worked out. I would really appreciate if anyone can aid me with the solutions.

raw cookie Value = .sampleCookie=eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjEiLCJoyA;

split cookie value = eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjEiLCJoyA

CodePudding user response:

driver.manage().getCookieNamed function gives you an instance of org.openqa.selenium.Cookie which doesn't have split() function

If you want to continue with this approach you need to cast it to String first:

String[] cookie = rawValue.toString().split(";");

Alternatively you can use Cookie class functions instead of "splitting", i.e.

  • rawValue.getName()
  • rawValue.getValue()
  • rawValue.getDomain()
  • rawValue.getExpiry()
  • rawValue.isSecure()
  • rawValue.isHttpOnly()

The same is for printing it to the log file, you cannot put a Cookie object there, you need to change it to be a String:

WDS.log.info(rawValue.toString());

Also your approach of writing the variables into the file won't work if you intend to run your script with 2 or more users, the target file will have corrupt data due to race conditions so consider switching to Sample Variables and Flexible File Writer instead.

  • Related