Home >Web Front-end >CSS Tutorial >How to test an element's CSS properties using Protractor?
Testing CSS properties is critical to ensuring the quality of your web application. CSS properties determine how elements appear on a web page, such as font size, color, and layout. Testing CSS properties can help detect errors and ensure that your application looks and functions as expected. A tool called Protractor provides developers with different ways to test CSS properties.
Protractor is a popular end-to-end testing framework that uses WebDriver to automate interactions between web applications and browsers. It is widely used for testing Angular applications, but can be used for testing other web applications as well.
In this article, we will learn how to test the CSS properties of an element with the help of Protractor. We will learn different ways to perform testing operations.
Using Protractor to test the CSS properties of an element requires the following steps.
To use Protractor, make sure it is installed on your system along with the required dependencies.
Install Protractor -
npm install -g protractor
Update binaries -
webdriver-manager update
Run the server -
webdriver-manager start
The conf.js file in the Protractor project is a configuration file that contains various settings and options for the Protractor test suite. Let's create a file called conf.js
exports.config = { seleniumAddress: 'http://localhost:4444/wd/hub', specs: ['spec.js'], capabilities: { browserName: 'chrome' }, onPrepare: function () { browser.manage().window().maximize(); }, jasmineNodeOpts: { showColors: true, defaultTimeoutInterval: 30000 }, baseUrl: 'file://' + __dirname + '/', onPrepare: function () { browser.resetUrl = 'file://'; } };
After setting up Protractor, create a new test specification file with any name, such as test.js, etc. We can create a new file in the specs directory of the Protractor project.
describe('Test CSS property of an element', () => { it('should have the correct color', () => { browser.get('https://tutorialspoint.com'); const element = element(by.css('.test-class)); expect(element.getCssValue('color')).toEqual('rgba(53, 163, 59, 0.2)'); }); });
In the above code, we use the class test-class to test the color attribute of the element. We expect the color attribute to evaluate to rgba(53, 163, 59, 0.2).
<html> <head> <title>Testing</title> </head> <body> <!-- Test element --> <div class="test-class" style="color: rgba(53, 163, 59, 0.2)"> Inner text </div> </body> </html>
To run the test, use the following command in the terminal -
protractor conf.js --suite css-property
In the above command, conf.js is the configuration file of the Protractor project, and --suite css-property specifies that only tests in the css-property suite should be run.
After running the test, you can view the test results in the terminal. If the test passes, you will see a message similar to this -
Test CSS properties of elements
✓ Should have the correct color
1 specification, 0 failures
The first method provided by Protractor is the getCssValue() method, which is used to obtain the calculated value of the CSS property of the element. This method takes the name of a CSS property as a parameter and returns its calculated value. Here is the syntax and examples -
The following is the syntax for testing CSS properties using Protractor’s getCssValue() method.
const element = element(by.css('.test-class')); expect(element.getCssValue('color')).toEqual('rgba(53, 163, 59, 0.2)');
In the given example, we use the class test class to test the color attribute of the element. The expected calculated value for the color property is rgba(53, 163, 59, 0.2).
describe('Test CSS property of an element using getCssValue()', () => { it('should have the correct color', () => { browser.get('https://example.com'); const element = element(by.css('.test-class')); element.getCssValue('color').then(function(value) { expect(value).toEqual('rgba(53, 163, 59, 0.2)'); }); }); });
The second way to test the CSS attributes of an element is to use the getAttribute() method to get the value of the element's style attribute. The style attribute contains the inline style applied to the element. Here is the syntax and examples -
The following is the syntax for testing CSS attributes using Protractor's getAttribute() method.
const element = element(by.css('.test-class')); expect(element.getAttribute('style')).toContain('color: green;');
In the given example, we are testing whether the style attribute of the element of class test-class contains the CSS property color: green;
describe('Test CSS property of an element using getAttribute()', () => { it('should have the correct color', () => { browser.get('https://example.com'); const element = element(by.css('.test-class')); element.getAttribute('style').then(function(value) { expect(value).toContain('color: green); }); }); });
The third method that can be used to test CSS properties is the browser.executeScript() method, which executes JavaScript code in the browser context and obtains the calculated value of the CSS property. Here is the syntax and examples -
The following is the syntax for testing CSS properties using Protractor's browser.executeScript() method.
const element = element(by.css('.test-class')); const color = browser.executeScript('return window.getComputedStyle(arguments[0]).getPropertyValue("color");', element); expect(color).toEqual('rgba(53, 163, 59, 0.2)');
In the given example, we execute JavaScript code in the browser context to get the calculated value of the color attribute of the element with the test class class. Here we use the window.getCompulatedStyle() method to get the calculated style of the element, and the getPropertyValue() method to get the value of the color property.
describe('Test CSS property of an element using browser.executeScript()', () => { it('should have the correct color', () => { browser.get('https://example.com'); const element = element(by.css('.test-class')); browser.executeScript('return window.getComputedStyle(arguments[0]).color;', element).then(function(value) { expect(value).toEqual('rgba(53, 163, 59, 0.2)'); }); }); });
测试元素的 CSS 属性是确保应用程序具有视觉吸引力和功能性的重要步骤。一个非常重要的工具 Protractor 用于以有效的方式执行此类测试,以测试使用 getCssValue() 和 getAttribute() 方法的元素的 CSS 属性。在本文中,我们了解了进行测试的完整步骤,现在如果您已按照本文中概述的步骤进行操作,则可以轻松设置 Protractor 并创建测试规范来测试元素的 CSS 属性。事实证明,使用 Protractor 测试 Web 应用程序(包括 Angular 应用程序)是可靠且高效的。有了这些知识,我们就可以编写有效的端到端测试,涵盖 Web 应用程序功能的所有方面,包括视觉外观。
The above is the detailed content of How to test an element's CSS properties using Protractor?. For more information, please follow other related articles on the PHP Chinese website!