使用量角器获取背景颜色

Obtain background colour using protractor

本文关键字:背景 颜色 获取 量角器      更新时间:2023-09-26

我是量角器的新手,我想做的是获得元素的背景颜色。

到目前为止,我有:

var colour = element(by.css("div[class='ng-isolate-scope my-form']")).getCssValue('background-color');
console.log(colour);

我试过使用"背景色"answers"背景颜色"。

上述控制台日志如下

 ...{ ptor_: 
   { controlFlow: [Function],
     schedule: [Function],
     setFileDetector: [Function],
     getSession: [Function],
     getCapabilities: [Function],
     quit: [Function],
     actions: [Function],
     touchActions: [Function],
     executeScript: [Function],
     executeAsyncScript: [Function],
     call: [Function],
     wait: [Function],
     sleep: [Function],
     getWindowHandle: [Function],
     getAllWindowHandles: [Function],
     getPageSource: [Function],
     close: [Function],
     getCurrentUrl: [Function],
     getTitle: [Function],
     findElementInternal_: [Function],
     findDomElement_: [Function],
     findElementsInternal_: [Function],
     takeScreenshot: [Function],
     manage: [Function],
     switchTo: [Function],
     driver: 
      Object {
        session_: [Object],
        executor_: [Object],
        flow_: [Object],
        fileDetector_: null },
     element: { [Function] all: [Function] },
     '$': [Function],
     '$$': [Function],
     baseUrl: '',
     rootEl: 'body',
     ignoreSynchronization: true,
     getPageTimeout: 10000,
     params: {},
     ready: 
      Object {
        closure_uid_262032795: 12,
        flow_: [Object],
        stack_: null,
        parent_: null,
        callbacks_: null,
        state_: 'fulfilled',
        handled_: true,
        value_: null,
        queue_: null },
     plugins_: 
      { pluginConfs: [],
        pluginObjs: [],
        assertions: {},
        resultsReported: false },
     resetUrl: 'data:text/html,<html></html>',
     trackOutstandingTimeouts_: true,
     mockModules_: [ [Object] ],
     allScriptsTimeout: 11000,
     getProcessedConfig: [Function],
     forkNewDriverInstance: [Function],
     restart: [Function] },
  parentElementArrayFinder: 
   { ptor_: 
      { controlFlow: [Function],
        schedule: [Function],
        setFileDetector: [Function],
        getSession: [Function],
        getCapabilities: [Function],
        quit: [Function],
        actions: [Function],
        touchActions: [Function],
        executeScript: [Function],
        executeAsyncScript: [Function],
        call: [Function],
        wait: [Function],
        sleep: [Function],
        getWindowHandle: [Function],
        getAllWindowHandles: [Function],
        getPageSource: [Function],
        close: [Function],
        getCurrentUrl: [Function],
        getTitle: [Function],
        findElementInternal_: [Function],
        findDomElement_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: '',
        rootEl: 'body',
        ignoreSynchronization: true,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function] },
     getWebElements: [Function],
     actionResults_: 
      Object {
        closure_uid_262032795: 4569,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: null,
        state_: 'pending',
        handled_: false,
        value_: undefined,
        queue_: null },
     locator_: Object { using: 'css selector', value: 'body[class=''ng-scope'']' } },
  then: [Function],
  elementArrayFinder_: 
   { ptor_: 
      { controlFlow: [Function],
        schedule: [Function],
        setFileDetector: [Function],
        getSession: [Function],
        getCapabilities: [Function],
        quit: [Function],
        actions: [Function],
        touchActions: [Function],
        executeScript: [Function],
        executeAsyncScript: [Function],
        call: [Function],
        wait: [Function],
        sleep: [Function],
        getWindowHandle: [Function],
        getAllWindowHandles: [Function],
        getPageSource: [Function],
        close: [Function],
        getCurrentUrl: [Function],
        getTitle: [Function],
        findElementInternal_: [Function],
        findDomElement_: [Function],
        findElementsInternal_: [Function],
        takeScreenshot: [Function],
        manage: [Function],
        switchTo: [Function],
        driver: [Object],
        element: [Object],
        '$': [Function],
        '$$': [Function],
        baseUrl: '',
        rootEl: 'body',
        ignoreSynchronization: true,
        getPageTimeout: 10000,
        params: {},
        ready: [Object],
        plugins_: [Object],
        resetUrl: 'data:text/html,<html></html>',
        trackOutstandingTimeouts_: true,
        mockModules_: [Object],
        allScriptsTimeout: 11000,
        getProcessedConfig: [Function],
        forkNewDriverInstance: [Function],
        restart: [Function] },
     getWebElements: [Function],
     actionResults_: 
      Object {
        closure_uid_262032795: 4569,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: null,
        state_: 'pending',
        handled_: false,
        value_: undefined,
        queue_: null },
     locator_: Object { using: 'css selector', value: 'body[class=''ng-scope'']' } } }

我所追求的只是元素的背景颜色,然后我会将其与我认为应该是的进行比较。

我试图获得的元素背景色是

<div class="ng-isolate-scope my-form" ng-show="!model.loading && activeTab == 'form' && model.formData != null && model.formData != undefined" ng-class="model.formData.className" auto-height="" style="height: 144px;">

如有任何帮助,我们将不胜感激。

您需要解析promise才能获得值。如果您想使用console.log打印它,请执行以下操作:

$('div.my-form').getCssValue('background-color').then(function(bgColor) {
  console.log(bgColor);
});

只是添加到@Andres的答案中,并考虑到:

我所追求的只是元素的背景颜色,然后我会将其与我认为应该是的颜色进行比较

由于您需要比较背景颜色,请注意,Protractor中的expect()知道什么是promise,并且可以在进行期望检查之前明确解决它。换句话说,如果您只需要比较promise的已解析值,则不需要使用then()

expect($('div.my-form').getCssValue('background-color')).toEqual("rbga(255, 255, 255)");