Tips for creating a Selenium Java logic that dynamically switches between two paths depending on the type of question

Currently, I am developing a logic in selenium-java to automate the process of taking tests. During this testing phase, each test-taker may receive either set-1 or set-2 at random for each node.

  1. The number of questions in each set may vary
  2. Answer data will be retrieved from an excel sheet (Attached Excel file)

I would appreciate any suggestions on the most effective approach to switch between set-1 and set-2 based on the given scenario. I have not written any code yet to share as I have never encountered this type of situation before.

Thank you all in advance for your help.

https://i.sstatic.net/CIDXp.png

Answer №1

Importing data from Excel and passing it to the method

To start, each set of data should be placed on its own Excel sheet.

https://i.sstatic.net/ZJFqE.png

Sheet name: Dataset1

Column name: Column1

The values in the column (excluding the column name) are extracted for use. TestNG then iterates through this data in the test method.

The next step involves fetching the data from Excel.

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaseTestProvider {
        Object[][] dataSupplierBase(String path, String sheetname) throws IOException {

        File file = new File(path); //Name of the FILE
        FileInputStream fis = new FileInputStream(file);

        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet = wb.getSheet(sheetname);//Name of the SHEET
        wb.close();
        int lastRowNum = sheet.getLastRowNum();
        int lastCellNum = sheet.getRow(0).getLastCellNum();
        Object[][] obj = new Object[lastRowNum][1];

        for (int i = 0; i < lastRowNum; i++) {
            Map<Object, Object> datamap = new HashMap<>();
            for (int j = 0; j < lastCellNum; j++) {
                datamap.put(sheet.getRow(0).getCell(j).getStringCellValue(), sheet.getRow(i + 1).getCell(j).getStringCellValue());
            }
            obj[i][0] = datamap;
        }
        return obj;
    }
}

The third part involves setting up TestNG's data provider.

public class TestCaseDataProvider extends BaseTestProvider {
    @DataProvider(name = "test1")
    public Object[][] dataSupplier1() throws IOException {
        String sheetname = "Dataset1";
        return dataSupplierBase("path to excel file", sheetname);
    }

Afterwards, utilize maps to extract data from specific sheets and columns.

@Test(dataProvider = "test1", dataProviderClass = TestCaseDataProvider.class)
public void testMethod(Map<Object, Object> map) {
    System.out.println(String.valueOf(map.get("Column1")));
    
}

By changing the data provider name manually or via code, you can seamlessly switch between different datasets.

Unresolved: Collating all columns at once and manipulating rows.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring Parquet Files with Node.js

Looking for a solution to read parquet files using NodeJS. Anyone have any suggestions? I attempted to use node-parquet but found it difficult to install and it struggled with reading numerical data types. I also explored parquetjs, however, it can only ...

Clicking the jAuery selection button on the website allows

I have a table with buttons that I need to customize for selection, using CSS. The goal is to style the buttons differently than standard checkboxes or radio buttons by using the type "button" and applying CSS/CSS3 styles. <div class="button"> ...

The table from datatables.net is failing to load with Ajax requests

Having trouble with v1.10 Datatables.net table not loading via ajax? The examples and documentation at datatables.net didn't provide a solution. Despite the table displaying, it shows "No data available in table". This issue is occurring in an older M ...

Using JQuery to load a table and inject it with html()

I am looking to populate an HTML table within a specific div element. The HTML code is being loaded using the following jQuery function: $("#table_wrapper").hide(); $.get("<?echo base_url();?>schichtplan/employee_fields/"+plan_id+"true",function(da ...

Retrieve the maximum number of slots from a JSON file and implement them into the

I've encountered an issue with my upload form. After uploading and previewing an image, I want to add it to a list as long as the list still has available slots. Here's an example: If the maximum number of slots is set to 5, then you can add up ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

Color the column of our kendo ui grid in gray

Within this kendo ui grid here, the initial column [OrderID] cannot be modified. I am seeking a solution to visually distinguish all disabled columns by applying a subtle gray shade, allowing users to easily identify them as non-editable. ...

Submitting a form that was loaded using jQuery cannot be done with jQuery

After successfully using jQuery to load a form from the sidebar to the main page, I encountered an issue with submitting the form using jQuery as well. Although I have previously used the same jQuery code to submit forms that were not loaded with jQuery, t ...

Enhancing the angular-ui-bootstrap typeahead module

Currently, I am using the typeahead feature from angular-ui-bootstrap to allow users to select a person's name or add a new name if it is not available in the options. I have customized the getMatchesAsync function with the following code: if(scope ...

Identifying when a browser is closed with multiple tabs open in Internet Explorer

I need to detect when a browser tab is closed in order to trigger a Struts action. I have successfully implemented this for a single tab using the following JavaScript code: <script type="text/javascript> function loadOut() { if ((window.event.c ...

The attempt to execute 'removeChild' on 'Node' was unsuccessful because parameter 1 is not the correct type. Although it did remove the elements from the DOM, it only did so once

It's quite a challenge I'm facing!!! Although there have been similar questions asked before, they were all for specific scenarios. I am currently developing a tictactoe game using the module design pattern. The function below helps me create tw ...

Managing Time Before Browser Refresh in AngularLearn how to monitor and examine the time remaining before

In my Angular web application, I am facing an issue where the user's login status is lost every time the browser is refreshed or reloaded. I want to implement a feature that allows the login status to remain active for a short period of time after the ...

Revamp the layout of the lower HTML video menu

Here is my code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> video { width: 100%; height: auto; } </style> </head> <body> <video width="400" controls> ...

VueJS Components experiencing issues with displaying images

Recently, I delved into learning VueJS and successfully created a basic restaurant menu page all within a single file. Excited by my progress, I decided to refactor the project using vue-cli, but hit a snag with the images not displaying properly. The cur ...

What could be causing my array to update when the method is called live but not during unit tests?

One of my methods is called toggleSelect which adds and removes items from an array named selectedItems. This method functions perfectly when I test it live in the browser. However, when running unit tests, it does not seem to work as expected. Despite cal ...

Looking for a dropdownlist with checkboxes in asp.net mvc but without the use of Entity Framework

I am in need of a dropdown list with checkboxes in asp.net mvc. The issue is that my view already has a layout page with existing script files, which seem to be causing a disturbance in the original layout. I have tried various solutions such as rearrangi ...

Is your jQuery 1.10.2 selector malfunctioning in Internet Explorer?

Having a problem with this simple jQuery click intercept not working in Internet Explorer when using jQuery 1.10.2: HTML <ul id="nav"> <li> <a href="/test">simple selector</a> </li> </ul> JS $(documen ...

Click the raised button in Material UI to open the "choose file" dialog box

In my React project, I have a floating button (Material UI) and I want to trigger the "choose file" dialog box whenever it is clicked. So far, I haven't been able to find a solution for this without using jQuery. </div> <input id ...

Overcoming Time Limits: What are the best strategies for maximizing performance in my JavaScript code?

I am currently tackling the challenge of solving the Sum of Subarray Ranges problem on LeetCode. Unfortunately, I am encountering a Time Limit Exceeded error which suggests that my code might not be optimized efficiently enough. As I am still learning, I w ...

Angular form for updating values

In my Angular project, I am utilizing an angular dynamic form where I am populating values using JSON. data = { firstName:"Eliseo", lastName:"Plunker", myArray:[ { emailAddress:"<a href='/cdn-cgi/l/emai ...