Can I get element value in selenium?

I want to get a value from one input in selenium. But I need this value in other project test in the same solution. Can I get this value in this case or I need run both test project at the same time to get it?

This is my variable (I have it in a class project)

public static string strNumeroCotizacion;

I get the value here in a test project called "Cotizacion":

Variables_RW.element = Variables_RW.driver.FindElement(By.XPath("//INPUT[@id='numeroCotizacion']"));
Variables_RW.strNumeroCotizacion = Variables_RW.element.GetAttribute("value").ToString();

And I need use it in this case in other project called "facturacion" in the same solution:

Variables_RW.driver.FindElement(By.XPath("//INPUT[@id='sCotizacion']")).SendKeys("strNumeroCotizacion"); 
Variables_RW.driver.FindElement(By.XPath("//INPUT[@id='sCotizacion']")).SendKeys(Keys.Enter);

Upvotes: 1

Views: 145

Answers (1)

Gaj Julije
Gaj Julije

Reputation: 2183

Interesting question. I don't think you can use variable from the different projects.

But you can write the value of that variable in the file when running your project1 and after you can read it in project 2. It depends on which language you are using in selenium.

This is the Java Solution

To write to the file:

               @SuppressWarnings("unchecked")
                public void customerDataWrite(String custID, String OrderNum, String Contact) {
                                // First OrderData
                                JSONObject createdOrderData = new JSONObject();
                                createdOrderData.put("customer", custID);
                                createdOrderData.put("orderName", OrderNum);
                                createdOrderData.put("contactName", Contact);

                                // Add order numbers to list
                                JSONArray OrderDataList = new JSONArray();
                                OrderDataList.add(createdOrderData);

                                // Write JSON file
                                try (FileWriter file = new FileWriter("orderdata.json")) {

                                                file.write(OrderDataList.toJSONString());
                                                file.flush();

                                } catch (IOException e) {
                                                e.printStackTrace();
                                }
                }

To read from the file:

                                JSONParser parser = new JSONParser();
                                try {                        
                                
                                                Object obj = parser.parse(new FileReader("orderdata.json"));
                                
                                // Get order object
                                                JSONArray array = (JSONArray) obj;
                                                                JSONObject jsonObject = (JSONObject) array.get(0);

                                                                // Get order contact name
                                                                orderContactName = (String) jsonObject.get("contactName");
                                                                System.out.println(orderContactName);

                                                                // Get company name
                                                                orderCustomerName = (String) jsonObject.get("customer");
                                                                System.out.println(orderCustomerName);

                                                     // Get order Order name
                                                                orderOrderName = (String) jsonObject.get("orderName");
                                                                System.out.println(orderOrderName);

}

Upvotes: 1

Related Questions