我想使用InnerText Value在赛普拉斯中进行进一步的计算

时间:2019-12-11 20:03:24

标签: web-applications ui-automation cypress

因此,我能够获取元素的innerText值,但是我想全局使用此值。我尝试使用以下代码:

cy.get('#selector').invoke('text').then(text => {
const price = text;
});

在其余测试中如何获取价格值?另外,该值是一个字符串,如何将其更改为整数?

2 个答案:

答案 0 :(得分:1)

您可以通过在commands.js文件中创建一个函数并为每个测试调用此函数来实现此目的。 在commands.js文件中,创建一个“ simplePrice()”函数,如下所示:

Cypress.Commands.add('simplePrice', () => {
    cy.get('#selector').invoke('val').then(val => {
        const price = val;
        return price;
      })
});

然后在每个测试中,您都可以得到如下的价格值。这种方式的好处是您可以拨打任意多次电话以获得价格值。还要将其保留为一项通用功能,当您在一个地方(即commands.js)进行更改时,所有测试都会收到更改。

describe('Test to get the price value', ()=>{  
    it('Test-1', () => {
        cy.simplePrice().then((val)=>{
            const price_first = val;
            console.log("Hello log this one:"+price_first);  
        })  
    })

    it('Test-2', () => {
        cy.simplePrice().then((val)=>{
            const price_second = val;
            console.log("Hello value this second time:"+price_second);
        }) 
    })

})

答案 1 :(得分:0)

根据我的观察,恒定价格只能在该功能中使用。您需要将其设为全局/模块化

def GoogleAPIWatchView(request):

    SCOPES = ['https://www.googleapis.com/auth/calendar']
    SERVICE_ACCOUNT_FILE = BASE_DIR + 'path_to_json'

    credentials = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_FILE, scopes=SCOPES)

    regex = re.compile('^HTTP_')
    headers = dict((regex.sub('', header), value) for (header, value) 
           in request.META.items() if header.startswith('HTTP_'))

    print(headers)
    print(request)

    return JsonResponse('success', status=200, safe=False)