简体   繁体   中英

How to get conditional row element in cypress

I am testing my application with cypress. For example I have 4 Values in my Table.

I want to get a row having on specific value in this case cypress-farm, and then I want to get the id column of that row which has cypress-farm, but cypress takes every id column inside my table. Can someone show me a pointer how to do it? What do I miss The value "selector" in this case is the id column the part where my question is the lines above cy.request

public static addproductToFarm(selector:string, productName:string) {
        cy.get(".list").then(($temp1) =>{
            cy.get(".list")
                .contains("cypress-farm")
                .get(selector).then(($temp)=>{
                const txt = $temp.text()

                cy.request({
                    url: Cypress.env("api_server") + "products/",
                    method: 'POST',
                    form: false,
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8',
                    },
                    body: {
                        availability: true,
                        category: {
                            hasOthers: false,
                            id: 1,
                            indexRanking: 0,
                            name: "Obst",
                            shown: true,
                            subCategories: [
                                null
                            ]
                        },
                        delivery: true,
                        description: productName,
                        descriptionExcerpt: "Cypress Product",
                        farmId: txt,
                        name: "Apple by Postman",
                        pickUp: true,
                        price: 10.00,
                        quantity: 10,
                        storageLimit: 10,
                        unit: "kg",
                        visibility: true
                    }


                })

            })
        } )

        return this;

    }

I think you might want .find(selector) instead of .get(selector) .

get() starts from the document, but find() starts from the previous subject (the result of .contains("cypress-farm") ).

See Get vs Find

The cy.get command always starts its search from the cy.root element. In most cases, it is the document element, unless used inside the .within() command. The .find command starts its search from the current subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM