Angular 7 : Unit test Jasmine automate window size on mobile and web

标签 angular karma-jasmine angular7

我需要编写一个自动化测试用例( Jasmine 测试),它将在 chrome 中运行,针对移动和 Web View 具有不同的分辨率 例如,它应该自动调整窗口大小 在 jasmine 中运行时 W: 375 H: 678 不幸的是,我无法找到解决方案来做到这一点

it('It should set sizes according to mobile', async(() => {

     //below code is not working
     //browser.manage().window().setSize(375, 678);
     let innerHeight = window.innerHeight;
     neither it won't work following code
     window.resizeTo(375, 678);

  }));

最佳答案

终于我找到了这个问题的答案

可以通过安装npm install karma-viewport来完成

这将向测试公开全局变量视口(viewport),从而允许设置视口(viewport)的尺寸

需要在 karma.conf.js 中配置视口(viewport)大小,如下

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ["viewport"]

    // Viewport configuration
    viewport: {
      breakpoints: [
        {
          name: "mobile",
          size: {
            width: 320,
            height: 480
          }
        },
        {
          name: "tablet",
          size: {
            width: 768,
            height: 1024
          }
        },
        {
          name: "screen",
          size: {
            width: 1440,
            height: 900
          }
        }
      ]
    }
  })
}

然后在规范文件中您可以访问屏幕尺寸

 it('should load the mobile view ', () => {
     viewport.set('mobile');
  });

关于 Angular 7 : Unit test Jasmine automate window size on mobile and web,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56003138/

相关文章:

angular - mat-datepicker 添加 touchUI 条件

gruntjs - Karma 从 0.12 升级到 0.13 后测试失败

AngularJS karma Jasmine 错误: Cannot Read Property of Undefined

angular - 既然 ProvideIn 是一个选项,那么 CoreModule 模式是否有理由?

angular - 订阅时返回。 ...类型 'Subscription' 缺少以下属性

c# - 将 CSV 文件从 Angular7 传递到 MVC .Net Core

node.js - 使用nodejs将图像上传到文件服务器并存储图像的url

angular - 将分页添加到表格 Angular 2

angular - 安装angular2 RC4路由器时找不到兼容版本: @angular/router@^2. 0.0-rc.4错误

javascript - 引用错误 : module is not defined - Karma/Jasmine configuration with Angular/Laravel app