Ragav
Ragav

Reputation: 5

How to capture datalayer object and print in my eclipse console using selenium

I need to capture the datalayer from the console tab in the chrome developer tools


Upvotes: 0

Views: 665

Answers (2)

Ragav
Ragav

Reputation: 5

Im using the following code @Zakaria Shahed , pls make correction and how to use assertion here?

    List<String>actual_values=new ArrayList <String>();
    List<String>Notexist=new ArrayList <String>();
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    List<String> datalist = new ArrayList<String>();
    datalist =  (List<String>) executor.executeScript("return window.dataLayer");
    //System.out.println(datalist);
    String DLexpected_values=data[6];// i will read DLexpected values from excel sheet - data[6]
    List<String> splitword2 = Arrays.asList(DLexpected_values.split(","));
    
    for(String value :splitword2) {
        boolean b = false;
        for (String entry : datalist) {
            if(entry.toString().contains(value)) {
                actual_values.add(value);
                b = true;
                break; 
            }
            
        }
        if(!b) {
            if(!Notexist.contains(value)) {
                Notexist.add(value);
            }
        }
    }
    if(splitword2.equals(actual_values1)) {
        
        System.out.println("Expected_Values:"+" "+splitword2);
        System.out.println("Actual_Values  :"+" "+actual_values);
        System.out.println("Status         :      Pass");
        
    }
    else {
        
        System.out.println("Expected_Values:"+" "+splitword2);
        System.out.println("Actual_Values  :"+" "+actual_values);
        System.out.println("Missing_Values :"+" "+Notexist);
        System.err.println("Status         :      Fail");
        
    }
    

Upvotes: 0

Zakaria Shahed
Zakaria Shahed

Reputation: 2707

Below code will help you to get data layer information

   JavascriptExecutor executor= (JavascriptExecutor)getDriver();

    ArrayList<Map<String, List<String> >> datalist = new ArrayList<>();

    //Execute google tag manage in the console using javascript executor to fetch values       
    datalist =  (ArrayList) executor.executeScript("return window.dataLayer");

    // Parse through GTM arrayList  
    for(int a=0; a < datalist .size(); a++) {
        for (String key : datalist .get(a).keySet()) {
            System.out.println(key + "      " + datalist .get(a).get(key));

        }
    }

Upvotes: 0

Related Questions