CodeGrade Help
Go to websiteAPI docsContact us
  • 🏠CodeGrade Help Center
  • ❓FAQ
    • Using CodeGrade in Safari
    • Using sections
    • Configuring roles of members
    • Archiving your course
    • Adding new members
    • Releasing grades and feedback
    • Exporting Submissions
    • Choosing a grading scale
    • Creating course-wide snippets
    • Course Gradebook
    • Allowing students to hand in after the deadline
  • Use built-in content
    • 👥Community Library
    • 🐍Introduction to Python Curriculum
  • Create your own assignments
    • 1️⃣Build your assignment
      • ➕Create assignment
        • ➕Standalone
        • ➕In Blackboard
        • ➕In Brightspace
        • ➕In Canvas
        • ➕In Moodle
        • ➕In Sakai
        • ➕In Open edX
        • 💾Importing a previous assignment
      • ⚙️General settings
      • 📋Add Rubric
      • 🤖Add Automatic Grading
      • 🧑‍🎓Student View
    • 2️⃣Grade your assignment
      • ✏️Give Feedback
      • 💯Give a Grade
    • 3️⃣Analyze your assignment
      • 🕵️‍♂️Detect Plagiarism
      • 📊Analytics
      • 🎓View and export grades
    • *️⃣Other features
      • AI Assistant
      • 👥Peer Feedback
      • 🫂Group Assignments
      • 🙋Collaborative Grading
      • 🕶️Anonymous Grading
      • 🛡️Manage Course Permissions
      • 📬Hand In Requirements
  • Automatic Grading Guides
    • 🏗️AutoTest V2 Blocks
    • ✏️Quizzes
      • ❓Multiple Choice Question
      • ❓Select All Question
      • 💻Coding Question
    • 🐍Python
      • 🐍Create your first Python assignment
      • 🐍Advanced Python autograding
    • ☕Java
      • ☕Create your first Java assignment
      • ☕Advanced Java autograding
    • 📘Jupyter Notebook
    • 🐬MySQL
    • 🌐Web Development
      • 🌐Web Development with Selenium
      • 🎨Automatically grading CSS
    • 🟨JavaScript
      • 🟨Create your first JavaScript assignment
      • 🟨Advanced JavaScript autograding
    • 📊R
    • ©️C
      • ©️Create your first C assignment
      • ©️Advanced C Autograding
    • 🖥️C#
      • 🖥️Create your first C# assignment
      • 🖥️Advanced C# autograding
    • ➕C++
      • ➕Create your first C++ assignment
      • ➕Advanced C++ autograding
    • 🐘PHP
      • 🐘Create your first PHP assignment
      • 🐘Advanced PHP autograding
    • 🏗️Code Structure Tests with Semgrep
  • For students
    • 🚀Getting started
      • 🚀Getting started in CodeGrade
      • 🚀Getting started in Blackboard
      • 🚀Getting started in Brightspace
      • 🚀Getting started in Canvas
      • 🚀Getting started in Moodle
      • 🤷‍♂️I forgot my CodeGrade username / password
    • 🧬Advanced Features
      • 👥Handing in with a group
      • 📥Handing in using Git
      • 📝Giving Peer Feedback
      • 🏆Doing a Final Exam
      • ❓Asking Questions
      • 💳Enrolling in a paid course
      • 🎟️Using a coupon to enroll in a course
      • ⏪Refunding a paid course
  • 📘APIv2: Typescript
  • 🐍APIv1: Python
  • 🤖APIv1: Docs
  • 🌐Our Website
  • ✉️Contact us
Powered by GitBook
On this page
  • Design Semgrep patterns for testing CSS rules
  • Instructions
  1. Automatic Grading Guides
  2. Web Development

Automatically grading CSS

In this page we describe how to automatically test css rules for web development assignments.

PreviousWeb Development with SeleniumNextJavaScript

Last updated 11 months ago

While in principle, it is possible to use to test the css properties defined for html elements, we advise against doing so in CodeGrade. The reason is that we run Selenium tests by using a web browser in headless mode, which can result in css rules working differently than they would while running your web browser with a normal display. Alternatively, we can use to test the structure of the css rules defined by the student.

Design Semgrep patterns for testing CSS rules

Semgrep is used to perform code analysis by writing syntax rules in a human-readable format. Rules are made by generic or language-specific patterns, which are then searched for in the student's code. In Semgrep's , you have:

  • Equivalences: Matching code that means the same thing even though it looks different;

  • Wildcards / ellipsis (...): Matching any statement, expression or variable;

  • Metavariables ($X): Matching unknown expressions that you do not yet know what they will exactly look like, but want to be the same variable in multiple parts of your pattern;

  • Logical operators: it is possible to logically combine simpler patterns to create more complex aggregate patterns.

Let's inspect an example of a Semgrep pattern that checks the properties of a btn css class:

patterns:
      - pattern-inside: |
         .btn { ... }
      - pattern: |
         ...
         margin: 20px;
         ...
      - pattern: |
          ...
          width: $ANYVALUE;
          ...
  • Line 1: with patterns we indicate that our overall pattern is composed of multiple sub-patterns;

  • Lines 2 to 3: we require that the patterns that will be defined later have to be found within the body of a class named btn;

  • Lines 4 to 7: we require that our pattern contains a margin setting of exactly 20 pixels;

  • Lines 8 to 11: we require that our pattern contains a width setting with any possible value (notice the use of a metavariable)

Instructions

  1. In the Setup section of your AutoTest, drag and drop a Script Block into your configuration and type the following bash commands:

    python -mvenv ~/.venv/semgrep_1.43.0
    source ~/.venv/semgrep_1.43.0/bin/activate
    python -m pip install semgrep==1.43.0
  2. In the Tests section of your AutoTest, drag and drop a Code Structure Test block. Specify the name of the CSS file you want to test in the "Student file" field.

  3. Drag and drop a Positive Match block into your configuration and nest it within the Code Structure Test block. Copy and paste your Semgrep rule in the provided editor.

  4. Build and Publish your snapshot.

We suggest using the online to quickly develop new rules for testing css rules.

🌐
🎨
Selenium
Semgrep
pattern syntax
Semgrep editor
A Semgrep test for Css rules. The test configuration is hidden to the students and the test is connected to a rubric category.