测试应用中所有页面的页面标题

时间:2022-11-02 20:08:05

So I recently implemented a bit of code that assigns page titles to the pages in my app via the controller.

所以我最近实现了一些代码,通过控制器将页面标题分配给我的应用程序中的页面。

In the controller:

在控制器中:

def index
    @page_title = "My Awesome Index Page!"
end

In the main layout:

在主要布局中:

<title><%= @page_title ? "#{@page_title} - Website Name" : "Website Name" %></title>

The goal is to give pages names like "My Awesome Index Page! - Website Name", but in the case that @page_title is not defined in the controller, it just defaults to "Website Name"

目标是给页面名称“My Awesome Index Page! - Website Name”,但是如果@page_title没有在控制器中定义,它只是默认为“Website Name”

Now to the issue. This app has somewhere on the order of 250 pages. I would like some sort of automated way to test all the pages and make sure that I haven't missed any. Ideally I would like to have an automated test that hits every page so that when future pages are added, if @page_title is not defined, the test will fail and alert me that the new page needs to have its page_title set.

现在来看问题。这个应用程序有250页的订单。我想要某种自动化方式来测试所有页面,并确保我没有错过任何页面。理想情况下,我希望有一个自动测试可以访问每个页面,以便在添加未来页面时,如果未定义@page_title,测试将失败并提醒我新页面需要设置其page_title。

Alternatively, it would be great to at least have a script of some kind that I could run that hits each page in the app and returns a list of the page titles or something. That way I can at least make sure that I got every one of them.

或者,至少有一个我可以运行的脚本,它会点击应用程序中的每个页面并返回页面标题列表或者其他内容,这将是很棒的。这样我至少可以确保我得到了每一个。

2 个解决方案

#1


0  

I don't have an answer to your question but I use a different approach.

我对你的问题没有答案,但我采用了不同的方法。

Usually best practice is to test exactly one thing at once and it has to be important. If it would be important to me to have a page title I would write a test for each title in question and so following the same approach as Micheal Hartl in his Rails Tutorial

通常最好的做法是一次测试一件事,它必须是重要的。如果有一个页面标题对我来说很重要的话我会为每个标题编写一个测试,所以遵循与他的Rails教程中的Micheal Hartl相同的方法

I definitly didn't write 250 specs only for page titles yet. But I never had the requirements.

我绝对没有为页面标题编写250个规格。但我从未有过要求。

#2


0  

My approach would be to use I18n, so you can record all titles in a locale file /config/locales/titles.yml. It allow you to group all titles in one single file, making easy to see what you are doing. It also have the advantage to keep your controller clean.

我的方法是使用I18n,因此您可以在区域设置文件/config/locales/titles.yml中记录所有标题。它允许您将所有标题分组到一个文件中,从而轻松查看您正在执行的操作。它还具有保持控制器清洁的优点。

Then to get your title you could use controller_name and action_name values to retrieve the text from the locale file. Wrapped in a helper method:

然后,要获取标题,可以使用controller_name和action_name值从区域设置文件中检索文本。包含在辅助方法中:

Helper method

辅助方法

# This is pseudo code, I did not test it, it is only to demonstrate the approach
# But it should work :)
def html_title
  title = I18n.translate! "#{controller_name}.#{action_name}"
  content_tag :title, title
end

titles.yml

titles.yml

en:
  users:
    index: 'Here you go with the list of awesome users'
    show: 'Showing an awesome user record'

Solution 1:

解决方案1:

As you can see I use I18n.translate! with a bang!, this will raise an exception if the translation is not found in your locale file: each title that you forgot to define will raise an exception.

如你所见,我使用I18n.translate!如果在你的语言环境文件中找不到翻译,这将引发异常:你忘记定义的每个标题都会引发异常。

As for the testing I would be using capybara to browse all pages and check that there is no error.

至于测试,我将使用capybara浏览所有页面并检查没有错误。

Solution 2:

解决方案2:

If you don't want the exception you can use :default option of translate like this:

如果你不想要例外,你可以使用:如下所示的翻译默认选项:

def html_title
  title = I18n.translate "#{controller_name}.#{action_name}", default: 'My awesome default title'
  content_tag :title, title
end

Note that I am not using the bang anymore. Then your Capybara scan all your pages and ensure that the title is not strictly equal to 'My awesome default title'

请注意,我不再使用爆炸了。然后你的Capybara扫描你的所有页面,并确保标题不严格等于'我真棒的默认标题'

#1


0  

I don't have an answer to your question but I use a different approach.

我对你的问题没有答案,但我采用了不同的方法。

Usually best practice is to test exactly one thing at once and it has to be important. If it would be important to me to have a page title I would write a test for each title in question and so following the same approach as Micheal Hartl in his Rails Tutorial

通常最好的做法是一次测试一件事,它必须是重要的。如果有一个页面标题对我来说很重要的话我会为每个标题编写一个测试,所以遵循与他的Rails教程中的Micheal Hartl相同的方法

I definitly didn't write 250 specs only for page titles yet. But I never had the requirements.

我绝对没有为页面标题编写250个规格。但我从未有过要求。

#2


0  

My approach would be to use I18n, so you can record all titles in a locale file /config/locales/titles.yml. It allow you to group all titles in one single file, making easy to see what you are doing. It also have the advantage to keep your controller clean.

我的方法是使用I18n,因此您可以在区域设置文件/config/locales/titles.yml中记录所有标题。它允许您将所有标题分组到一个文件中,从而轻松查看您正在执行的操作。它还具有保持控制器清洁的优点。

Then to get your title you could use controller_name and action_name values to retrieve the text from the locale file. Wrapped in a helper method:

然后,要获取标题,可以使用controller_name和action_name值从区域设置文件中检索文本。包含在辅助方法中:

Helper method

辅助方法

# This is pseudo code, I did not test it, it is only to demonstrate the approach
# But it should work :)
def html_title
  title = I18n.translate! "#{controller_name}.#{action_name}"
  content_tag :title, title
end

titles.yml

titles.yml

en:
  users:
    index: 'Here you go with the list of awesome users'
    show: 'Showing an awesome user record'

Solution 1:

解决方案1:

As you can see I use I18n.translate! with a bang!, this will raise an exception if the translation is not found in your locale file: each title that you forgot to define will raise an exception.

如你所见,我使用I18n.translate!如果在你的语言环境文件中找不到翻译,这将引发异常:你忘记定义的每个标题都会引发异常。

As for the testing I would be using capybara to browse all pages and check that there is no error.

至于测试,我将使用capybara浏览所有页面并检查没有错误。

Solution 2:

解决方案2:

If you don't want the exception you can use :default option of translate like this:

如果你不想要例外,你可以使用:如下所示的翻译默认选项:

def html_title
  title = I18n.translate "#{controller_name}.#{action_name}", default: 'My awesome default title'
  content_tag :title, title
end

Note that I am not using the bang anymore. Then your Capybara scan all your pages and ensure that the title is not strictly equal to 'My awesome default title'

请注意,我不再使用爆炸了。然后你的Capybara扫描你的所有页面,并确保标题不严格等于'我真棒的默认标题'